asSQL

Blog | MXML Example | Actionscript Example | Example App | FAQ
Known Issues | Discussion | Documentaton | Downloads | Source Code


UPDATE, July 15 2007: Now on Google Code

I have create a google code project for asSQL, the next release is on there now. Currently only the SWC's are up there, I will be working on documentation, source, examples etc. throughout the next week or so.

You can find the project here: http://code.google.com/p/assql/


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.

When developing this I have been testing on a locally installed MySQL 5 database. I have been developing it to support 4.1.1 and later MySQL releases but I have not done any testing on 4.1.1. If you come across any bugs please post them in the forum.

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();
}

 

Example App

I am still working with my ISP to come to a solution to get a crossdomain.xml file put on the database servers. I will post an example application as soon as things are worked out.

 

Known Issues

- Sometimes you will receive a 'Bad Handshake' error when connecting with correct credentials. Not sure why this is happening but it is something I need to look into.

- SQLErrorEvents sometimes are not caught by the MysqlService class.

 

FAQ

Does it work in Apollo? You Bet!
How do I handle sandbox security?
Is there a server scrambler example available?

 

Discussion

Forum

 

Documentation

ASDoc's

 

Downloads

Latest Release on Google Code: http://code.google.com/p/assql/

Alpha Release

 

Source Code

View Online / Download Here