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

You also want an ePaper? Increase the reach of your titles

YUMPU automatically turns print PDFs into web optimized ePapers that Google loves.

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

Working with local SQL databases in AIR<br />

Using synchronous database operations<br />

Adobe AIR 1.0 and later<br />

There is little differ<strong>en</strong>ce in the actual code that you use to execute and respond to operations wh<strong>en</strong> using synchronous<br />

execution, compared to the code for asynchronous execution mode. The key differ<strong>en</strong>ces betwe<strong>en</strong> the two approaches<br />

fall into two areas. The first is executing an operation that dep<strong>en</strong>ds on another operation (such as SELECT result rows<br />

or the primary key of the row added by an INSERT statem<strong>en</strong>t). The second area of differ<strong>en</strong>ce is in handling errors.<br />

Writing code for synchronous operations<br />

Adobe AIR 1.0 and later<br />

The key differ<strong>en</strong>ce betwe<strong>en</strong> synchronous and asynchronous execution is that in synchronous mode you write the code<br />

as a single series of steps. In contrast, in asynchronous code you register ev<strong>en</strong>t list<strong>en</strong>ers and oft<strong>en</strong> divide operations<br />

among list<strong>en</strong>er methods. Wh<strong>en</strong> a database is connected in synchronous execution mode, you can execute a series of<br />

database operations in succession within a single code block. The following example demonstrates this technique:<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, Op<strong>en</strong>Mode.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 />

As you can see, you call the same methods to perform database operations whether you’re using synchronous or<br />

asynchronous execution. The key differ<strong>en</strong>ces betwe<strong>en</strong> the two approaches are executing an operation that dep<strong>en</strong>ds on<br />

another operation and handling errors.<br />

Last updated 6/6/2012<br />

753

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

Saved successfully!

Ooh no, something went wrong!