Coldfusion Tutorials



Spry Tutorials

Air Tutorials


Synchronous 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 connecting synchronously is easier (I do anyway) to code, unless you come from an Action Script background to begin with.

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(true);
         
         private function init():void{
            //create a new db-file in the application storage location    
var file:File = new File("app-storage:/myDB.db");
            try{
            //open the db file
            sqlConnection.open(file);
            }
            catch(error:SQLError){
            //catch any errors             Alert.show(error.message);
            }
            //check if its connected             if (sqlConnection.connected){
               
             Alert.show("Database Connected");
            }else{
             Alert.show("Opps, database is not connected!");
            }
            
         }
      ]]>
   </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 true in the constructor means that we want to open the database for synchronous access, the default is asynchronous. 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. We then pass the file reference to the open method of our SQLConnection and it should open our newly created database. Just for saftey reasons I wrapped the open command in a try/catch block, if anything bad happens an Alert box will be displayed with the error.

Running the Application

When you run the application you should see the following.