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

As a g<strong>en</strong>eral rule, any object that you store in a database using an INSERT statem<strong>en</strong>t is returned as an instance of the<br />

same data type wh<strong>en</strong> you execute a SELECT statem<strong>en</strong>t. However, the data type of the retrieved value can be differ<strong>en</strong>t<br />

dep<strong>en</strong>ding on the affinity of the database column in which the value is stored. Wh<strong>en</strong> a value is stored in a column, if<br />

its data type doesn’t match the column’s affinity, the database attempts to convert the value to match the column’s<br />

affinity. For example, if a database column is declared with NUMERIC affinity, the database attempts to convert inserted<br />

data into a numeric storage class (INTEGER or REAL) before storing the data. The database throws an error if the data<br />

can’t be converted. According to this rule, if the String “12345” is inserted into a NUMERIC column, the database<br />

automatically converts it to the integer value 12345 before storing it in the database. Wh<strong>en</strong> it’s retrieved with a SELECT<br />

statem<strong>en</strong>t, the value is returned as an instance of a numeric data type (such as Number) rather than as a String instance.<br />

The best way to avoid undesirable data type conversion is to follow two rules. First, define each column with the affinity<br />

that matches the type of data that it is int<strong>en</strong>ded to store. Next, only insert values whose data type matches the defined<br />

affinity. Following these rules provides two b<strong>en</strong>efits. Wh<strong>en</strong> you insert the data it isn’t converted unexpectedly (possibly<br />

losing its int<strong>en</strong>ded meaning as a result). In addition, wh<strong>en</strong> you retrieve the data it is returned with its original data type.<br />

For more information about the available column affinity types and using data types in SQL statem<strong>en</strong>ts, see the “Data<br />

type support” on page 1114.<br />

Using synchronous and asynchronous database<br />

operations<br />

Adobe AIR 1.0 and later<br />

Previous sections have described common database operations such as retrieving, inserting, updating, and deleting<br />

data, as well as creating a database file and tables and other objects within a database. The examples have demonstrated<br />

how to perform these operations both asynchronously and synchronously.<br />

As a reminder, in asynchronous execution mode, you instruct the database <strong>en</strong>gine to perform an operation. The<br />

database <strong>en</strong>gine th<strong>en</strong> works in the background while the application keeps running. Wh<strong>en</strong> the operation finishes the<br />

database <strong>en</strong>gine dispatches an ev<strong>en</strong>t to alert you to that fact. The key b<strong>en</strong>efit of asynchronous execution is that the<br />

runtime performs the database operations in the background while the main application code continues executing.<br />

This is especially valuable wh<strong>en</strong> the operation takes a notable amount of time to run.<br />

On the other hand, in synchronous execution mode operations don’t run in the background. You tell the database<br />

<strong>en</strong>gine to perform an operation. The code pauses at that point while the database <strong>en</strong>gine does its work. Wh<strong>en</strong> the<br />

operation completes, execution continues with the next line of your code.<br />

A single database connection can’t execute some operations or statem<strong>en</strong>ts synchronously and others asynchronously.<br />

You specify whether a SQLConnection operates in synchronous or asynchronous wh<strong>en</strong> you op<strong>en</strong> the connection to<br />

the database. If you call SQLConnection.op<strong>en</strong>() the connection operates in synchronous execution mode, and if you<br />

call SQLConnection.op<strong>en</strong>Async() the connection operates in asynchronous execution mode. Once a<br />

SQLConnection instance is connected to a database using op<strong>en</strong>() or op<strong>en</strong>Async(), it is fixed to synchronous or<br />

asynchronous execution.<br />

Last updated 6/6/2012<br />

752

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

Saved successfully!

Ooh no, something went wrong!