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 example demonstrates accessing the primary key of an inserted row in asynchronous execution mode:<br />

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

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

insertStmt.execute();<br />

function resultHandler(ev<strong>en</strong>t:SQLEv<strong>en</strong>t):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 id<strong>en</strong>tifier 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 g<strong>en</strong>erated 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 sc<strong>en</strong>es the database g<strong>en</strong>erates an integer row id<strong>en</strong>tifier value for the row.<br />

That g<strong>en</strong>erated value is the value of the lastInsertRowID property.<br />

The value is always the row id<strong>en</strong>tifier of the most-rec<strong>en</strong>tly inserted row. If an INSERT statem<strong>en</strong>t causes a trigger to<br />

fire which in turn inserts a row, the lastInsertRowID property contains the row id<strong>en</strong>tifier of the last row inserted<br />

by the trigger rather than the row created by the INSERT statem<strong>en</strong>t.<br />

As a consequ<strong>en</strong>ce 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. Ev<strong>en</strong> if your table does not include an explicit INTEGER PRIMARY KEY column, it is<br />

equally acceptable to use the database-g<strong>en</strong>erated row id<strong>en</strong>tifier as a primary key for your table in the s<strong>en</strong>se of defining<br />

relationships with related tables. The row id<strong>en</strong>tifier column value is available in any SQL statem<strong>en</strong>t 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 6/6/2012<br />

747

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

Saved successfully!

Ooh no, something went wrong!