Article written

  • on 15.05.2010
  • at 11:07 PM
  • by Pedram

Powerful ActionScript with MySQL 1

May15

ActionScript MySQL
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!

var connection:Connection = new Connection(SERVER_ADDRESS, PORT, DB_USER, DB_PASS, DB_NAME);
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.

var st:Statement = connection.createStatement();
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.

var token:MySqlToken = st.executeQuery();
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.

subscribe to comments RSS

There is one comment for this post

  1. Very nice article..
    :)

Please, feel free to post your own comment

* these are required fields

Pedram Pourhossein is powered by WordPress and FREEmium Theme.

1|2|3|4|5|6|7|8|9|10|11|12|13|14|15|16|17|18|19|20|21|22|23|24|25|26|27|28|29|30|31|32|33|34|35|36|37|38|39|40|41|42|43|44|45|46|47|48|49|50|51|52|53|54|55|56|57|58|59|60|61|62|63|64|65|66|67|68|69|70|71|72|73|74|75|76|77|78|79|80|81|82|83|84|85|86|87|88|89|90|91|92|93|94|95|96|97|98|99|100|101|102|103|104|105|106|107|108|109|110|111|112|113|114|115|116|117|118|119|120|121|122|123|124|125|126|127|128|129|130|131|132|133|134|135|136|137|138|139|140|141|142|143|144|145|146|147|148|149|150|151|152|153|154|155|156|157|158|159|160|161|162|163|164|165|166|167|168|169|170|171|172|173|174|175|176|177|178|179|180|181|182|183|184|185|186|187|188|189|190|191|192|193|194|195|196|197|198|199|200|201|202|203|204|205|206|207|208| viagra in contrassegno viagra prices baltimore roaton viagra no prescription canada online pharmacy canada cialis soft tabs pharmacy buy viagra & dapoxetine with viagra with dapoxetine cheap buy viagra professional buy cheap viagra professional viagra professional online in usa uk is levitra sold at walmart buy viagra xanax cheapest in uk viagra cilais tablets buy tetracycline without prescription cipla viagra order cialis online us kamagra oral gel online australia cialis black in australia viagram tablete gnc viagra buying viagra in a storeAccutane Online Doxycycline online Buy Cheap Lexapro Online No Prescription Prednisone Online payday loans online no checking account