Adobe Integrated Runtime (AIR) for JavaScript Developers Pocket Reference

(nextflipdebug5) #1
Embedded Database | 117

SQLStatementclass executes commands against a specified
database.


As database transactions all happen asynchronously, the best
place to check for any required schema—or to create it—is
in the handler for theSQLEvent.OPENevent. At this point, the
application can be assured a connection against which state-
ments can be executed. Along the same lines, event handlers
must also be registered on theSQLStatement instance:


var stmt = new air.SQLStatement();

stmt.addEventListener( air.SQLErrorEvent.ERROR,
doStmtError );
stmt.addEventListener( air.SQLEvent.RESULT, doStmtResult );

When applied to aSQLStatementobject, theSQLErrorEvent.
ERRORevent is called when there has been an error while exe-
cuting a SQLStatement.next() or SQLStatement.execute()
method. Conversely, the SQLEvent.RESULT event is called
when results are returned from the database. This usually
indicates a successful execution:


function doStmtError( event )
{
alert( "There has been a problem executing the statement" );
}

function doStmtResult( event )
{
alert( "The database table has been created." );
}

In order to execute a SQL statement, a SQLConnection
instance against which to execute must be established. A
SQLConnectioninstance can be assigned to theSQLStatement.
sqlConnectionproperty. TheSQLStatement.textproperty is
then assigned any SQL that needs to be executed. Finally, the
SQLStatement.execute() method is called:


stmt.sqlConnection = db;
stmt.text = "CREATE TABLE IF NOT EXISTS contact ( " +
"id INTEGER PRIMARY KEY AUTOINCREMENT, " +
"first TEXT, " +
Free download pdf