My Books

  • Follow me on Twitter
My Photo

Subscribe my blog

  • Get this widget from Widgetbox
  • Add to Google
  • RSS FEEDS
  • Enter your Email here to subscribe :



    Powered by FeedBlitz

Blog Widget by LinkWithin

« Flash on the Beach 2008 schedule is up and I'll be there as a speaker | Main | I'm now an official Adobe Flex Certified Instructor (ACI) »

How to create an in-memory database in AIR using ActionScript and JavaScript

One of my favourite AIR features is the support of creating local database file using SQLite embedded engine. If you have SQL skills it'll be easy to storing persistent local data  to your desktop application. You can use this feature to store application data, favourite users' configuration options, document-oriented application, to cache data and synch it with the network.
Creating a SQLite database in AIR is pretty simple:

1. Create a local file with the .db extension using the File class:

// JavaScript
var myDB = air.File.desktopDirectory.resolvePath("db/myDBFile.db");

// ActionScript
var folder:File= File.applicationStorageDirectory.resolvePath( "db" );                  
folder.createDirectory();            
_myDB     = folder.resolvePath( "myDBFile.db" );

2. Create an istance of the SQLConection class to open the database file to work with and open the database using the synchronous method open() or the asyncronous method openAsync():

//JavaScript

var dbConn = new air.SQLConnection();
dbConn.openAsync(myDB);

dbConn.addEventListener(air.SQLEvent.OPEN, onOpenHandler);
dbConn.addEventListener(air.SQLErrorEvent.ERROR, onErrorHandler);

// ActionScript

private var _dbConn:SQLConnection(); = new SQLConnection();

_dbConn.openAsync(_myDB);

_dbConn.addEventListener(SQLEvent.OPEN, openHandler);
_dbConn.addEventListener(SQLErrorEvent.ERROR, errorHandler);

If everything is ok the AIR SQLite database is created and opened. Now you're ready to create tables and populate it.
But a cool thing about the open() or the asyncronous method openAsync() is that you can create an in-memory database passing a null value to the first parameter of these methods. It means that the database is created not in a database file on disk but it's a temporary database reference.
The openAsync() method (as well as the opee())  has the following syntax:

public function openAsync(reference:Object = null, openMode:String = "create", responder:Responder = null, autoCompact:Boolean = false, pageSize:int = 1024):void

where the reference parameter is an Object that specifies the location of the database file that is opened. If you set it to null you'll create an in-memory database:

// actionScript

private var _dbConn:SQLConnection(); = new SQLConnection();

_dbConn.openAsync(null);


//JavaScript

var dbConn = new air.SQLConnection();
dbConn.openAsync(null);

So you can now create your SQL statements, define mechanism for executing multiple statements in a transaction, use  the begin(), commit(), and rollback() methods without having a local file. At the end of your operations you'll decide if storing the database on the local client or sending it to your remote server !

Comments

As I was reading that "At the end of your operations you'll decide if storing the database on the local client or sending it to your remote server", I started to look for an example how to store the in-memory database on the filesystem but could not find one. Could you give a hint how to do that? Thank you.

Verify your Comment

Previewing your Comment

This is only a preview. Your comment has not yet been posted.

Working...
Your comment could not be posted. Error type:
Your comment has been posted. Post another comment

The letters and numbers you entered did not match the image. Please try again.

As a final step before posting your comment, enter the letters and numbers you see in the image below. This prevents automated programs from posting comments.

Having trouble reading this image? View an alternate.

Working...

Post a comment

Pistach.io

Speaker at

WebDeveloper's Journal Author

  • My favourite Flash Lite 3 mobile phone
  • Web Developer's & Designer's Journal by Sys Con Media
  • Web Developer's & Designer's Journal Blogger
    Web Developer's & Designer's Journal by Sys Con Media
  • FullAsGoog Aggregator
  • Macromedia WebLogs Aggregator

June 2009

Sun Mon Tue Wed Thu Fri Sat
  1 2 3 4 5 6
7 8 9 10 11 12 13
14 15 16 17 18 19 20
21 22 23 24 25 26 27
28 29 30