29.04.2013 Views

ACTIONSCRIPT 3.0

Create successful ePaper yourself

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

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

Working with local SQL databases in AIR<br />

The following example demonstrates accessing the primary key of an inserted row in asynchronous execution mode:<br />

insertStmt.text = "INSERT INTO ...";<br />

insertStmt.addEventListener(SQLEvent.RESULT, resultHandler);<br />

insertStmt.execute();<br />

function resultHandler(event:SQLEvent):void<br />

{<br />

// get the primary key<br />

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

}<br />

var primaryKey:Number = result.lastInsertRowID;<br />

// do something with the primary key<br />

The following example demonstrates accessing the primary key of an inserted row in synchronous execution mode:<br />

insertStmt.text = "INSERT INTO ...";<br />

try<br />

{<br />

insertStmt.execute();<br />

// get the primary key<br />

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

var primaryKey:Number = result.lastInsertRowID;<br />

// do something with the primary key<br />

}<br />

catch (error:SQLError)<br />

{<br />

// respond to the error<br />

}<br />

Note that the row identifier may or may not be the value of the column that is designated as the primary key column<br />

in the table definition, according to the following rules:<br />

If the table is defined with a primary key column whose affinity (column data type) is INTEGER, the<br />

lastInsertRowID property contains the value that was inserted into that row (or the value generated by the<br />

runtime if it’s an AUTOINCREMENT column).<br />

If the table is defined with multiple primary key columns (a composite key) or with a single primary key column<br />

whose affinity is not INTEGER, behind the scenes the database generates an integer row identifier value for the row.<br />

That generated value is the value of the lastInsertRowID property.<br />

The value is always the row identifier of the most-recently inserted row. If an INSERT statement causes a trigger to<br />

fire which in turn inserts a row, the lastInsertRowID property contains the row identifier of the last row inserted<br />

by the trigger rather than the row created by the INSERT statement.<br />

As a consequence of these rules, if you want to have an explicitly defined primary key column whose value is available<br />

after an INSERT command through the SQLResult.lastInsertRowID property, the column must be defined as an<br />

INTEGER PRIMARY KEY column. Even if your table does not include an explicit INTEGER PRIMARY KEY column, it is<br />

equally acceptable to use the database-generated row identifier as a primary key for your table in the sense of defining<br />

relationships with related tables. The row identifier column value is available in any SQL statement by using one of the<br />

special column names ROWID, _ROWID_, or OID. You can create a foreign key column in a related table and use the row<br />

Last updated 4/22/2013<br />

748

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

Saved successfully!

Ooh no, something went wrong!