Powerful ActionScript with MySQL 1

Hi,
I believe working with bytes was one of the best things in ActionScript 3.0 and it was a big jump in this language, but in these days it’s a usual stuff and many of AS3 developers have good knowledge about that, Although it’s really a deep sea and you need to know many things about that. (here’s a good resource for AS developers).
About three years ago when many of Flash developers wanted to learn new points of AS3, Matt MacLean made his ActionScript MySQL driver (at this time it needs some updates but unfortunately seems it’s no longer be developed), I can remember when I read Mike Chamber’s blog post for first time it was really interesting for me but now after about three years I felt I need this library for one of my AIR projects, In my opinion if you have a local project and you need to work with an enterprise database assql library is a very good choice to make direct connection between your application and database.
Anyway it’s a minimal example about a big library:
First you have to download assql library from SVN
After that you need a connection to use your database, it’s really easy; just enter db name, port (default mysql port is 3306), username and password!
connection.addEventListener(Event.CONNECT, handleConnected);
connection.connect();
In my case, this connection bothered me when I had more than 3 queries per second and I was receiving many timeout because every connection had a limited pool size.
But I made a simple connection class to solve this problem! I made about 20 connections (I emphasize it was a local database with one client), and this “improved connection manager” was finding and returning the freest connection, As a result I always had the fastest connection.
And then you should make your own statement to build your request, again it’s easy but you have to know you need to make a smart class to manage your queries.
st.sql = 'SELECT * FROM tblItems where PID=?';
st.setNumber(1, 15);
In second line we have just one ambiguous point PID=?, you should highlight your parameter(s) this way and finally you must set parameter(s) in query with setters like line 3, for example in that line we have set value 15 as our first parameter.
token.mynote = 'my note!';
token.addResponder(new AsyncResponder(result, fault, token));
private function result(data:Object, token:Object):void
{
var rs:ResultSet = ResultSet(data);
var arr:Array = [];
while(rs)
{
if(rs.next())
arr.push(rs);
else
break;
}
trae(arr);
}
private function fault(info:Object, token:Object):void
{
trace("Error: " + info + "Token" + token.mynote);
}
Finally we need to execute our query, our statement instance has an “executeQuery” method, it’s easy and I don’t like to write more than this about this simple process, just we have a little important thing in these lines, That’s “MySqlToken” token helps you to get and parse errors, results, and queries. It’s easy and I’m sure you can work with it very easily.
I think ORM in ActionScript is not a bad topic for my blog!
have a good time.
I'm Pedram Pourhossein, an ActionScript developer, interested in Flash/Flex and gonna write about my own experiences on Flash platform and technology in this blog.
Very nice article..