Switch On The Code RSS Button - Click to Subscribe
Jun
12

Adobe AIR and Flex - SQLConnection

flex icon

Adobe added the ability to use local databases when they created AIR. This is one of the many features that help make Adobe AIR a great solution for cross platform desktop applications. In this quick tutorial I am going to show how to create a database file and open a connection to it. The code is very simple so let's jump right into it.


The first thing is setting up a file for the database. In SQLite, which is the database engine used in Adobe AIR, the entire database is just stored in a single file. It is actually a really nice compact and portable system. I used the applicationStorageDirectory function on the File class to create a File that points to the local application storage and is named tut.db. In my case (in Vista) the actual path on disk resolves to C:\Users\User\AppData\Roaming\SQLConnectionTutorial\Local Store. The file isn't actually created until we make another call.

<?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 mx.controls.Alert;
     
      private var dbConn:SQLConnection;
      private var dbFile:File;
   
      private function init():void
      {
        dbFile =
          File.applicationStorageDirectory.resolvePath("tut.db");

        dbConn = new SQLConnection();
     
        try
        {
            dbConn.open(dbFile, SQLMode.CREATE);
        }
        catch(e:SQLError)
        {
          Alert.show("SQL Error Occured: ", e.message);
        }
      }
    ]]>
  </mx:Script>

</mx:WindowedApplication>

Above it the entire code for creating the database file and opening the connection. We use the open function on the SQLConnection class to open the connection. It is also advised to wrap any database functions in a try/catch block in order to catch any sql errors that may occur.

Once you have connected to the database you can do many things. Most of the operations are done using the SQLStatement class, which allows you to query the database for typical retrieving, inserting, updating, and deleting calls. One of very first things, though, is creating database table(s) and adding data. This is obviously needed before you can actually store/retrieve any data. The local database also has many other nuances that I will try to cover in further detail later on.

This is just quick intro to the local database support in Adobe AIR. Look forward to some more tutorials on the different aspects of the local databases. If you have any questions feel free to leave a comment.



Posted in Adobe AIR, Flex, All Tutorials by The Fattest |

3 Responses

  1. ofis mobilyaları Says:

    thanks.

  2. Khairunnisa Says:

    “The file isn’t actually created until we make another call.”

    What if I already have an sqlite database file, with data in it.. I don’t want to create but just call it.. How do I call it in Vista?

    Can I still use File.applicationStorageDirectory?

  3. The Fattest Says:

    Khairunnisa, that should work just fine. all the file directory calls are platform agnostic.

Leave a Comment

Please note: Comment moderation is enabled and may delay your comment. There is no need to resubmit your comment.

Powered by WP Hashcash