Adobe Integrated Runtime (AIR) for JavaScript Developers Pocket Reference

(nextflipdebug5) #1
Embedded Database | 115

Database transactions happen asynchronously, which
means an application must first create and register a han-
dler for the events in which it is interested. In the case of
establishing a connection to a database, theSQLEvent.OPEN
event will be monitored. Among various other properties,
theSQLEvent.type property can be used to determine the
status of the database.


db.addEventListener( air.SQLEvent.OPEN, doDbOpen );

function doDbOpen( event )
{
alert( "Connected" );
}

Calling theSQLConnection.open()method can take a number
of different arguments. The most common arguments are the
file reference to the database, and a Boolean value indicating
whether the database should be created if it does not already
exist. This simultaneously creates the database if it does not
exist, and then establishes a connection to the database:


db.open( file, true );

While the database will close automatically when the applica-
tion exists, developers should consider callingSQLConnection.
close() during the onunload event. The SQLConnection.
close()method takes no arguments. Taking the time to
manually close the database at the termination of the applica-
tion helps ensure that data is not accidentally corrupted, and
helps to maintain best practices:


<html>
<head>

<title>Connecting to a Database</title>
<script type="text/javascript" src="AIRAliases.js">
</script>

<script>
var db =new air.SQLConnection();

function doDbOpen( event )
Free download pdf