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 demonstrates the same techniques as the preceding one, but uses synchronous execution<br />

mode. The example 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 rows<br />

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

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

listing assumes there is a SQLConnection instance named conn that has already be<strong>en</strong> instantiated and is already<br />

connected to the database. It also assumes that the “employees” table has already be<strong>en</strong> 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.errors.SQLError;<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 />

try<br />

{<br />

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

selectStmt.execute();<br />

// access the result data<br />

var result:SQLResult = selectStmt.getResult();<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 />

}<br />

catch (error:SQLError)<br />

{<br />

trace("Error message:", error.message);<br />

trace("Details:", error.details);<br />

}<br />

Last updated 6/6/2012<br />

739

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

Saved successfully!

Ooh no, something went wrong!