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 />

Executing an operation that dep<strong>en</strong>ds on another operation<br />

Adobe AIR 1.0 and later<br />

Wh<strong>en</strong> you’re using synchronous execution mode, you don’t need to write code that list<strong>en</strong>s for an ev<strong>en</strong>t to determine<br />

wh<strong>en</strong> an operation completes. Instead, you can assume that if an operation in one line of code completes successfully,<br />

execution continues with the next line of code. Consequ<strong>en</strong>tly, to perform an operation that dep<strong>en</strong>ds on the success of<br />

another operation, simply write the dep<strong>en</strong>d<strong>en</strong>t code immediately following the operation on which it dep<strong>en</strong>ds. For<br />

instance, to code an application to begin a transaction, execute an INSERT statem<strong>en</strong>t, retrieve the primary key of the<br />

inserted row, insert that primary key into another row of a differ<strong>en</strong>t table, and finally commit the transaction, the code<br />

can all be writt<strong>en</strong> as a series of statem<strong>en</strong>ts. The following example demonstrates these operations:<br />

var conn:SQLConnection = new SQLConnection();<br />

// The database file is in the application storage directory<br />

var folder:File = File.applicationStorageDirectory;<br />

var dbFile:File = folder.resolvePath("DBSample.db");<br />

// op<strong>en</strong> the database<br />

conn.op<strong>en</strong>(dbFile, SQLMode.UPDATE);<br />

// start a transaction<br />

conn.begin();<br />

// add the customer record to the database<br />

var insertCustomer:SQLStatem<strong>en</strong>t = new SQLStatem<strong>en</strong>t();<br />

insertCustomer.sqlConnection = conn;<br />

insertCustomer.text =<br />

"INSERT INTO customers (firstName, lastName) " +<br />

"VALUES ('Bob', 'Jones')";<br />

insertCustomer.execute();<br />

var customerId:Number = insertCustomer.getResult().lastInsertRowID;<br />

// add a related phone number record for the customer<br />

var insertPhoneNumber:SQLStatem<strong>en</strong>t = new SQLStatem<strong>en</strong>t();<br />

insertPhoneNumber.sqlConnection = conn;<br />

insertPhoneNumber.text =<br />

"INSERT INTO customerPhoneNumbers (customerId, number) " +<br />

"VALUES (:customerId, '800-555-1234')";<br />

insertPhoneNumber.parameters[":customerId"] = customerId;<br />

insertPhoneNumber.execute();<br />

// commit the transaction<br />

conn.commit();<br />

Last updated 6/6/2012<br />

754

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

Saved successfully!

Ooh no, something went wrong!