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

Understanding statem<strong>en</strong>t parameters<br />

Adobe AIR 1.0 and later<br />

Frequ<strong>en</strong>tly an application uses a single SQL statem<strong>en</strong>t multiple times in an application, with slight variation. For<br />

example, consider an inv<strong>en</strong>tory-tracking application where a user can add new inv<strong>en</strong>tory items to the database. The<br />

application code that adds an inv<strong>en</strong>tory item to the database executes a SQL INSERT statem<strong>en</strong>t that actually adds the<br />

data to the database. However, each time the statem<strong>en</strong>t is executed there is a slight variation. Specifically, the actual<br />

values that are inserted in the table are differ<strong>en</strong>t because they are specific to the inv<strong>en</strong>tory item being added.<br />

In cases where you have a SQL statem<strong>en</strong>t that’s used multiple times with differ<strong>en</strong>t values in the statem<strong>en</strong>t, the best<br />

approach is to use a SQL statem<strong>en</strong>t that includes parameters rather than literal values in the SQL text. A parameter is<br />

a placeholder in the statem<strong>en</strong>t text that is replaced with an actual value each time the statem<strong>en</strong>t is executed. To use<br />

parameters in a SQL statem<strong>en</strong>t, you create the SQLStatem<strong>en</strong>t instance as usual. For the actual SQL statem<strong>en</strong>t assigned<br />

to the text property, use parameter placeholders rather than literal values. You th<strong>en</strong> define the value for each<br />

parameter by setting the value of an elem<strong>en</strong>t in the SQLStatem<strong>en</strong>t instance’s parameters property. The parameters<br />

property is an associative array, so you set a particular value using the following syntax:<br />

statem<strong>en</strong>t.parameters[parameter_id<strong>en</strong>tifier] = value;<br />

The parameter_id<strong>en</strong>tifier is a string if you’re using a named parameter, or an integer index if you’re using an unnamed<br />

parameter.<br />

Using named parameters<br />

Adobe AIR 1.0 and later<br />

A parameter can be a named parameter. A named parameter has a specific name that the database uses to match the<br />

parameter value to its placeholder location in the statem<strong>en</strong>t text. A parameter name consists of the “:” or “@” character<br />

followed by a name, as in the following examples:<br />

:itemName<br />

@firstName<br />

The following code listing demonstrates the use of named parameters:<br />

var sql:String =<br />

"INSERT INTO inv<strong>en</strong>toryItems (name, productCode)" +<br />

"VALUES (:name, :productCode)";<br />

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

addItemStmt.sqlConnection = conn;<br />

addItemStmt.text = sql;<br />

// set parameter values<br />

addItemStmt.parameters[":name"] = "Item name";<br />

addItemStmt.parameters[":productCode"] = "12345";<br />

addItemStmt.execute();<br />

Last updated 6/6/2012<br />

730

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

Saved successfully!

Ooh no, something went wrong!