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

You can choose to perform database operations asynchronously, meaning the database engine runs in the background<br />

and notifies you when the operation succeeds or fails by dispatching an event. You can also perform these operations<br />

synchronously. In that case the database operations are performed one after another and the entire application<br />

(including updates to the screen) waits for the operations to complete before executing other code. For more<br />

information on working in asynchronous or synchronous execution mode, see “Using synchronous and asynchronous<br />

database operations” on page 753.<br />

Connecting to a database<br />

Adobe AIR 1.0 and later<br />

Before you can perform any database operations, first open a connection to the database file. A SQLConnection<br />

instance is used to represent a connection to one or more databases. The first database that is connected using a<br />

SQLConnection instance is known as the “main” database. This database is connected using the open() method (for<br />

synchronous execution mode) or the openAsync() method (for asynchronous execution mode).<br />

If you open a database using the asynchronous openAsync() operation, register for the SQLConnection instance’s<br />

open event in order to know when the openAsync() operation completes. Register for the SQLConnection instance’s<br />

error event to determine if the operation fails.<br />

The following example shows how to open an existing database file for asynchronous execution. The database file is<br />

named “DBSample.db” and is located in the user’s “Pointing to the application storage directory” on page 672.<br />

import flash.data.SQLConnection;<br />

import flash.data.SQLMode;<br />

import flash.events.SQLErrorEvent;<br />

import flash.events.SQLEvent;<br />

import flash.filesystem.File;<br />

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

conn.addEventListener(SQLEvent.OPEN, openHandler);<br />

conn.addEventListener(SQLErrorEvent.ERROR, errorHandler);<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 />

conn.openAsync(dbFile, SQLMode.UPDATE);<br />

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

{<br />

trace("the database opened successfully");<br />

}<br />

function errorHandler(event:SQLErrorEvent):void<br />

{<br />

trace("Error message:", event.error.message);<br />

trace("Details:", event.error.details);<br />

}<br />

Last updated 4/22/2013<br />

726

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

Saved successfully!

Ooh no, something went wrong!