asSQL-AS3直接操作MySQL数据库

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:
复制内容到剪贴板
代码:

<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();
}
  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值