AS30连接MySQL的新方式(as3.0 mysql)

AS3.0连接MySQL的新方式

Adobe Flash Professional CS6以前的版本,曾经通过asapche本身集成的PHP、MySQL服务与Flash进行交互,以实现对MySQL数据库的读写操作。而从Adobe Flash Professional CS6及以后的版本开始,Adobe官方逐步推出一款新的数据库连接方式——通过Air自带的SQLite3来连接MySQL,通过调用第三方SQLite ODBC Driver的方式,从而实现与MySQL数据库交互。

这种新的连接方式,可以替代之前需要APACHE服务器援助的老旧方式,让Flash开发者在从Flex到Flash的过渡中,更加自由和灵活地完成数据交互。

以下是一段AS3.0代码示例,演示如何连接MySQL数据库:

    import adobe.utils.*;
import flash.data.*;
import flash.errors.*;
import flash.events.*;
import flash.filesystem.*;
import flash.net.*;
import flash.system.*;
import flash.utils.*;
import mx.utils.*;

public class MySQL
{
private var conn:SQLConnection;
private var statement:SQLStatement;

public function MySQL()
{
super();
this.conn = new SQLConnection();
this.statement = new SQLStatement();
}

public function connect(database:String, server:String, port:int, username:String, password:String) : void
{
var inAppStore:Boolean;
var file:File;
var dbFile:File;
var str:String;
var localConnection:LocalConnection;
var urlLoader:URLLoader;
var variables:URLVariables;
var request:URLRequest;
var event:IOErrorEvent;
var sqlError:SQLError;
var e:Error;
var settings:Object;
var sqlStatement:SQLStatement;
var database:String = database;
inAppStore = this.isInAppStore();
if (inAppStore)
{
file = File.applicationStorageDirectory;
}
else
{
file = File.documentsDirectory;
}
dbFile = file.resolvePath(database + ".db");
if (dbFile.exists)
{
if (inAppStore)
{
this.conn.open(dbFile, SQLMode.READ);
}
else
{
str = "file:" + dbFile.nativePath;
this.conn.open(new URLRequest(str), SQLMode.READ);
}
this.statement.sqlConnection = this.conn;
}
else
{
localConnection = new LocalConnection();
this.statement.sqlConnection = this.conn;
urlLoader = new URLLoader();
variables = new URLVariables();
request = new URLRequest("http://" + server + ":" + port + "/scripts/createDatabase.php");
variables.username = username;
variables.password = password;
variables.database = database;
request.method = URLRequestMethod.POST;
request.data = variables;
urlLoader.addEventListener(Event.COMPLETE, function (param1:Event) : void
{
statement.text = event.target.data;
try
{
statement.execute();
conn.close();
if (inAppStore)
{
conn.open(dbFile, SQLMode.READ);
}
else
{
str = "file:" + dbFile.nativePath;
conn.open(new URLRequest(str), SQLMode.READ);
}
initDatabase();
}
catch (sqlError:SQLError)
{
conn.close();
}
catch (e:Error)
{
conn.close();
trace("Error connecting to database in createDatabase.php. (" + e + ")");
}
return;
});
urlLoader.addEventListener(IOErrorEvent.IO_ERROR, function (param1:IOErrorEvent) : void
{
trace("Error connecting to database in createDatabase.php. (" + param1 + ")");
conn.close();
return;
});
try
{
urlLoader.load(request);
}
catch (e:Error)
{
trace("Error connecting to database in createDatabase.php. (" + e + ")");
conn.close();
}
}
return;
}

private function isInAppStore() : Boolean
{
var _loc_1:* = System.capabilities.version.toLowerCase().indexOf("ios");
var _loc_2:* = System.capabilities.version.toLowerCase().indexOf("and");
return _loc_1 > -1 || _loc_2 > -1;
}

private function initDatabase() : void
{
var sqlStatement:SQLStatement;
sqlStatement = new SQLStatement();
sqlStatement.sqlConnection = this.conn;
sqlStatement.text = "CREATE TABLE IF NOT EXISTS people (id INTEGER PRIMARY KEY AUTOINCREMENT, name VARCHAR, age INTEGER);";
sqlStatement.execute();
return;
}

}

通过以上代码示例,可以看到新的连接方式相比之前需要APACHE服务器援助的老旧方式,更加简洁、清晰,且使用了类似于本地文件方式的连接方式,方便开发者进行开发与测试。

总之Adobe Flash Professional CS6机身自带数据库连接方式,更加自由和灵活,更加方便改写已有代码和开发全新的Flash应用程序。


数据运维技术 » AS30连接MySQL的新方式(as3.0 mysql)