13.08.2012 Views

ACTIONSCRIPT 3 Developer’s Guide en

ACTIONSCRIPT 3 Developer’s Guide en

ACTIONSCRIPT 3 Developer’s Guide en

SHOW MORE
SHOW LESS

Create successful ePaper yourself

Turn your PDF publications into a flip-book with our unique Google optimized e-Paper software.

<strong>ACTIONSCRIPT</strong> 3.0 DEVELOPER’S GUIDE<br />

Working with local SQL databases in AIR<br />

The following code listing defines a SQLStatem<strong>en</strong>t instance whose text is a SELECT statem<strong>en</strong>t. The statem<strong>en</strong>t retrieves<br />

rows containing the firstName and lastName column values of all the rows of a table named employees. This<br />

example uses asynchronous execution mode. Wh<strong>en</strong> the execution completes, the selectResult() method is called,<br />

and the resulting rows of data are accessed using SQLStatem<strong>en</strong>t.getResult() and displayed using the trace()<br />

method. Note that this listing assumes there is a SQLConnection instance named conn that has already be<strong>en</strong><br />

instantiated and is already connected to the database. It also assumes that the “employees” table has already be<strong>en</strong><br />

created and populated with data.<br />

import flash.data.SQLConnection;<br />

import flash.data.SQLResult;<br />

import flash.data.SQLStatem<strong>en</strong>t;<br />

import flash.ev<strong>en</strong>ts.SQLErrorEv<strong>en</strong>t;<br />

import flash.ev<strong>en</strong>ts.SQLEv<strong>en</strong>t;<br />

// ... create and op<strong>en</strong> the SQLConnection instance named conn ...<br />

// create the SQL statem<strong>en</strong>t<br />

var selectStmt:SQLStatem<strong>en</strong>t = new SQLStatem<strong>en</strong>t();<br />

selectStmt.sqlConnection = conn;<br />

// define the SQL text<br />

var sql:String =<br />

"SELECT firstName, lastName " +<br />

"FROM employees";<br />

selectStmt.text = sql;<br />

// register list<strong>en</strong>ers for the result and error ev<strong>en</strong>ts<br />

selectStmt.addEv<strong>en</strong>tList<strong>en</strong>er(SQLEv<strong>en</strong>t.RESULT, selectResult);<br />

selectStmt.addEv<strong>en</strong>tList<strong>en</strong>er(SQLErrorEv<strong>en</strong>t.ERROR, selectError);<br />

// execute the statem<strong>en</strong>t<br />

selectStmt.execute();<br />

function selectResult(ev<strong>en</strong>t:SQLEv<strong>en</strong>t):void<br />

{<br />

// access the result data<br />

var result:SQLResult = selectStmt.getResult();<br />

}<br />

var numRows:int = result.data.l<strong>en</strong>gth;<br />

for (var i:int = 0; i < numRows; i++)<br />

{<br />

var output:String = "";<br />

for (var columnName:String in result.data[i])<br />

{<br />

output += columnName + ": " + result.data[i][columnName] + "; ";<br />

}<br />

trace("row[" + i.toString() + "]\t", output);<br />

}<br />

function selectError(ev<strong>en</strong>t:SQLErrorEv<strong>en</strong>t):void<br />

{<br />

trace("Error message:", ev<strong>en</strong>t.error.message);<br />

trace("Details:", ev<strong>en</strong>t.error.details);<br />

}<br />

Last updated 6/6/2012<br />

737

Hooray! Your file is uploaded and ready to be published.

Saved successfully!

Ooh no, something went wrong!