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

import flash.data.SQLConnection;<br />

import flash.data.SQLMode;<br />

import flash.filesystem.File;<br />

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

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

var <strong>en</strong>cryptionKey:ByteArray = new ByteArray();<br />

<strong>en</strong>cryptionKey.writeUTFBytes("Some16ByteString"); // This technique is not secure!<br />

try<br />

{<br />

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

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

}<br />

catch (error:SQLError)<br />

{<br />

if (error.errorID == 3138)<br />

{<br />

trace("Incorrect <strong>en</strong>cryption key");<br />

}<br />

else<br />

{<br />

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

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

}<br />

}<br />

For an example demonstrating a recomm<strong>en</strong>ded way to g<strong>en</strong>erate an <strong>en</strong>cryption key, see “Example: G<strong>en</strong>erating and<br />

using an <strong>en</strong>cryption key” on page 762.<br />

Changing the <strong>en</strong>cryption key of a database<br />

Adobe AIR 1.5 and later<br />

Wh<strong>en</strong> a database is <strong>en</strong>crypted, you can change the <strong>en</strong>cryption key for the database at a later time. To change a<br />

database’s <strong>en</strong>cryption key, first op<strong>en</strong> a connection to the database by creating a SQLConnection instance and calling<br />

its op<strong>en</strong>() or op<strong>en</strong>Async() method. Once the database is connected, call the re<strong>en</strong>crypt() method, passing the new<br />

<strong>en</strong>cryption key as an argum<strong>en</strong>t.<br />

Like most database operations, the re<strong>en</strong>crypt() method’s behavior varies dep<strong>en</strong>ding on whether the database<br />

connection uses synchronous or asynchronous execution mode. If you use the op<strong>en</strong>() method to connect to the<br />

database, the re<strong>en</strong>crypt() operation runs synchronously. Wh<strong>en</strong> the operation finishes, execution continues with the<br />

next line of code:<br />

var newKey:ByteArray = new ByteArray();<br />

// ... g<strong>en</strong>erate the new key and store it in newKey<br />

conn.re<strong>en</strong>crypt(newKey);<br />

On the other hand, if the database connection is op<strong>en</strong>ed using the op<strong>en</strong>Async() method, the re<strong>en</strong>crypt() operation<br />

is asynchronous. Calling re<strong>en</strong>crypt() begins the re<strong>en</strong>cryption process. Wh<strong>en</strong> the operation completes, the<br />

SQLConnection object dispatches a re<strong>en</strong>crypt ev<strong>en</strong>t. You use an ev<strong>en</strong>t list<strong>en</strong>er to determine wh<strong>en</strong> the re<strong>en</strong>cryption<br />

finishes:<br />

Last updated 6/6/2012<br />

760

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

Saved successfully!

Ooh no, something went wrong!