asSQL-AS3直接操作MySQL数据库
In Sept. 06 I set out to show someone that is was possible to connect to a mySQL server from acionscript. It took about an hour to get actionscript connecting to mySQL. I was getting a bad handshake error but if anything I proved that it could be done. That project then sat in Flex Builder until about a month ago when I started back on it. And now, a mySQL actionscript 3 driver exists.
This project is very much still in Alpha stages but neverless it is working. Writing a database driver in actionscript raises some major concerns. One of the biggest concerns to address is fact that to use an actionscript database driver you will need to make your database server publicly accessible. Another issue is where to store the server location, username's, password's etc.
For passwords, I just assumed that most people would not want to store the password in the swf itself. So when connecting to the database you provide a "Scrambler" class that handles receiving the seed from mysql, sending it somewhere, encrypting the seed using the database password, then sending the scramble back to the connection to be used in the authentication process. Currently the only scrambler I am providing is the "PlainTextScrambler" which will allows you to just provide a string for the password. This scrambler will work for embedding passwords in the swf and allowing the users to input a password.
Here are some quick examples of using the driver.
MXML:
.
Straight Actionscript:
This project is very much still in Alpha stages but neverless it is working. Writing a database driver in actionscript raises some major concerns. One of the biggest concerns to address is fact that to use an actionscript database driver you will need to make your database server publicly accessible. Another issue is where to store the server location, username's, password's etc.
For passwords, I just assumed that most people would not want to store the password in the swf itself. So when connecting to the database you provide a "Scrambler" class that handles receiving the seed from mysql, sending it somewhere, encrypting the seed using the database password, then sending the scramble back to the connection to be used in the authentication process. Currently the only scrambler I am providing is the "PlainTextScrambler" which will allows you to just provide a string for the password. This scrambler will work for embedding passwords in the swf and allowing the users to input a password.
Here are some quick examples of using the driver.
MXML:
复制内容到剪贴板
.
代码:
<mx:Script>
<![CDATA[
private function callService(sql:String):void
{
sqlService.send(sql);
}
]]>
</mx:Script>
<sql:MysqlService id="sqlService"
host="database.mydomain.com"
port="3306"
user="username"
scrambler="{new PlainTextScrambler('password')}"
database="databaseName"
response="onResponse(event)"
error="onError(event)" />
<mx:ComboBox id="cbx1" dataProvider="{sqlService.lastResult}" labelField="userName" />
.
Straight Actionscript:
复制内容到剪贴板
代码:
private function getUserList():void
{
var host:String = "database.mydomain.com";
var port:int = 3306;
var user:String = "mydatabaseuser";
var scrambler:PlainTextScrambler = new PlainTextScrambler("password");
var database:String = "databaseName";
var con:Connection = new Connection(host, post, user, scrambler, database);
con.addEventListener(Event.CONNECT, onConnect);
con.addEventListener(SQLErrorEvent.SQL_ERROR, onError);
con.connect();
}
private function onConnect(e:Event):void
{
var con:Connection = Connection(e.target);
var st:Statement = con.createStatement();
st.addEventListener(RestulsEvent.RESULTS, onResults); //FOR SELECT
st.addEventListener(ResponseEvent.RESPONSE, onResponse); //FOR INSERT, UPDATE, etc.
st.addEventListener(SQLErrorEvent.SQL_ERROR, onError);
st.executeQuery("SELECT * FROM users;");
}
private function onResults(e:ResultsEvent):void
{
var st:Statement = Statement(e.target);
var con:Connection = st.getConnection();
var rs:ResultSet = e.resultSet;
while ( rs.next() )
{
var userName = rs.getString("userName");
var email = rs.getString(2);
}
con.disconnect();
}
private function onResponse(e:ResponseEvent):void
{
var st:Statement = Statement(e.target);
var con:Connection = st.getConnection();
var affectedRows:int = e.affectedRows;
var insertID:int = e.insertID;
con.disconnect();
}
private function onError(e:SQLErrorEvent):void
{
var st:Statement = Statement(e.target);
var con:Connection = st.getConnection();
var message:String = e.msg;
var errorNo:int = e.id;
var text:String = e.text; // Equals SQLError #{id}: {msg}
con.disconnect();
}