Coldfusion Tutorials



Spry Tutorials

Air Tutorials


Asynchronous Database Connection (Flex)

There are two ways that you can connect to embedded SQLite databases in AIR, synchronously and asynchronously. You may find that in certain circumstances that you want to connect asynchronously this is especially useful if you will be doing a lot of batch execution of SQL statements and don't want to have the user wait for it to finish executing. This tutorial shows you how to connect to the embedded SQLite database asynchronously and differs only slightly from the synchronous tutorial.

The Code

Create a new AIR project using Flex Builder, and copy/paste the code below into the main .mxml page.

<?xml version="1.0" encoding="utf-8"?>
<mx:WindowedApplication xmlns:mx="http://www.adobe.com/2006/mxml" layout="absolute"
   creationComplete="init()">

   <mx:Script>
      <![CDATA[
         import flash.data.SQLConnection;
         import flash.filesystem.File;
         import mx.controls.Alert
         //set up a synchronous db connection          
private var sqlConnection:SQLConnection = new SQLConnection(false);
         
         private function init():void{
            //create a new db-file in the application storage location          
var file:File = new File("app-storage:/myDB.db");
            
            sqlConnection.addEventListener(SQLEvent.OPEN,dbOpenedHandler);
            sqlConnection.addEventListener(SQLErrorEvent.ERROR,dbErrorHandler);
            //open the db file
            sqlConnection.open(file);
         }
         private function dbOpenedHandler(event:SQLEvent):void{
            Alert.show("Database Opened");
         }
         private function dbErrorHandler(error:SQLError):void{
            Alert.show("Houston we have a problem:" + error.message);
         }
      ]]>
   </mx:Script>
</mx:WindowedApplication>

Dissecting the Code

In the <mx:WindowedApplication> tag we add a call to our init function when the creation complete event is fired this means that immediately after the application is initialized but before it is displayed our initialization function will be called. We then create an <mx:Script> block to write our ActionScript code. The first thing we do is add two import statements to get the appropriate references to the file and data packages. We need the reference to the file package in order to do any file access on the users local machine. The data package of course is needed to do anything with the embeded SQLite database. We also impor the mx.controls.Alert package for the alert box.

After we have imported the references we create a private variable that will hold our database Connection, the false in the constructor means that we want to open the database for asynchronous access, the default is asynchronous so we could easily have left the false out completely. The next thing we do is create our init function. We create a reference to a file on the file system, here if the file doesn't already exist it will be created thereafter it will be opened. The only "interesting" bit here is app-storage:, this is a AIR specific shorthand that refers to the filesystem location of where the AIR application has been installed regardless of which OS it has been installed on. Since we are going to be opening an asynchronous connection to the database the call to the open function will return immediately and our application will continue running. This means we need to create event listeners in order to find out when the database is actual open or if an error occurs during the open operation. We do this by adding two event listeners to our sqlConnection, one for the OPEN event and one to catch any errors. We of course then need to create our event handlers.

Running the Application

When you run the application you should see the following.