Abdulcode

Pages

Jul 20, 2010

Actionscript with MySQL

Today I walk thru the Database connectivity. At that time I got some of the interesting thing about the DB connectivity. ie) MySQL with Flex / AS3 / AIR.

By using the SWC and below mentioned sample code, we directly connect the MySQL with AS3.

Download the assql swc file.

Sample
<?xml version="1.0" encoding="utf-8"?>
<mx:Application xmlns:mx="http://www.adobe.com/2006/mxml"
layout="vertical"
initialize="init();">

<mx:Script>
<![CDATA[
import mx.collections.ArrayCollection;
import mx.controls.Alert;
import mx.rpc.AsyncResponder;

import com.maclema.mysql.Connection;
import com.maclema.mysql.MySqlToken;
import com.maclema.mysql.ResultSet;
import com.maclema.mysql.Statement;

private var con:Connection;

public function init():void
{
var SERVER_ADDRESS:String = "localhost";
var PORT:int = 3306;
var DB_USER:String = "root";
var DB_PASS:String = "root";
var DB_NAME:String = "test";

con = new Connection(SERVER_ADDRESS, PORT,
DB_USER, DB_PASS, DB_NAME);
con.addEventListener(Event.CONNECT, handleConnected);
con.connect();
}

private function handleConnected(e:*):void
{
execute();
}

private function execute():void
{
var query:String = "SELECT name FROM Names";
var st:Statement = con.createStatement();
st.sql = query;

var token:MySqlToken = st.executeQuery();
token.mynote = 'abdul';
token.addResponder(new AsyncResponder(result, fault, token));
}

[Bindable]
private var arr:ArrayCollection;

private function result(data:Object, token:Object):void
{
var rs:ResultSet = ResultSet(data);
arr = new ArrayCollection();

while (rs)
{
if (rs.next())
arr.addItem(rs.getString("name"));
else
break;
}
}

private function fault(info:Object, token:Object):void
{
trace("Error: " + info + "Token" + token.mynote);
}
]]>
</mx:Script>

<mx:List dataProvider="{arr}"
width="25%"
height="50%">
</mx:List>

</mx:Application>

1 comment:

Post a Comment