Adobe Integrated Runtime (AIR) for JavaScript Developers Pocket Reference

(nextflipdebug5) #1

120 | Chapter 4: AIR Mini-Cookbook


Discussion


Given a valid database file with the appropriate schema cre-
ated, SQL92 statements can be executed using the
SQLStatementobject. The sameSQLStatementobject can be
reused to execute multiple statements. When reusing the
sameSQLStatement, it’s important to differentiate what type
of statement has just been executed. There are various means
to accomplish listening for the different actions.


function doSave( )
{
var first = document.getElementById( "txtFirst" ).
value;
var last = document.getElementById( "txtLast" ).value;

stmt.text = "INSERT INTO contact VALUES ( " +
"NULL, " +
"'" + first + "', " +
"'" + last + "' )";
stmt.execute( );
}

One approach is to assign different event handlers for the dif-
ferent statements that will be executed. (Don’t forget to
remove the old handlers.) Another approach is to specify dif-
ferent responder objects that have been created with the spe-
cific statement in mind. The approach used in this example is
a basic state machine that tracks what type of statement has
just been executed:


var NONE = - 1;
var CREATE_SCHEMA = 0;
var INSERT_DATA = 1;

var state = NONE;

var stmt = new air.SQLStatement( );

// Other database creation and configuration

function doSave( )
{
var first = document.getElementById( "txtFirst" ).value;
var last = document.getElementById( "txtLast" ).value;
Free download pdf