« January 2008 | Main | March 2008 »

February 25, 2008

Flex 3 and AIR 1.0 released!

Check 'em out!

http://www.adobe.com/products/flex/

http://www.adobe.com/products/air/

And here's a good New York Times article about AIR: Adobe Blurs Lines Between PC and Web

February 12, 2008

Data storage and caching with SQLite databases and Adobe AIR

You may or may not know by now that the AIR runtime includes a version of SQLite engine. Being a smaller implementation of SQL, SQLite supports all your usual database transactions, a lot of the complex queries, and triggers. With it,you can create a database to store all the data for your Flex/AIR desktop applications, store data for offline use of your Flex/AIR desktop/web applications, or for caching of data. It's all done in the same way. I was recently working on an application where I wanted the user to have an option to be logged in automatically when the app was launched, so the first time the user logged in I created a database to cache that login info. From then on when the application is launched it checks to see if the database exists, and if so it grabs the login info, and logs them in automatically.

I've made a simple example which is a super simple desktop application which stores it's data in a SQLite database. The idea and implementation here is very similar to what I just described for the caching example. On launch, we first check to see if the database exists. If it does, that means it ought to contain some data so we grab the data and display it. If the database doesn't exist, we create it and add our one default entry, then load it into the application. The user can then add, remove, and update entries. When each of these transactions sends a result of success, we reload all the data in the database. Obviously in a real-world application this wouldn't be a great idea; that's too much overhead. One option would be to just manipulate the dataProvider ArrayCollection after each successful transaction -- but for this simple example I'm leaving it the way it is for the intent of simply demonstrating using SQLite.

At this point I think I'll let the code speak for itself.


ᅠ Basically you'll notice the basic steps are:

1. Create a connection: connection = new SQLConnection();
2. Define the database file: dbFile = File.applicationStorageDirectory.resolvePath(dbFileString);
3. Open (or create and open ) the database: connection.open(dbFile);
4. Create an empty SQLStatement: var sql : SQLStatement = new SQLStatement();
5. Create a query: var sqlString : String = "CREATE TABLE Users (" +
     " uid INTEGER PRIMARY KEY AUTOINCREMENT, " +
     " name TEXT, " +
     " phonenumber TEXT)";
6. Attach the connection and the query to the SQLStatement:
     sql.sqlConnection = connection;
     sql.text = sqlString;
7. Create event listeners for success and failure:
     sql.addEventListener(SQLEvent.RESULT, onDBCreateTableResult);
     sql.addEventListener(SQLErrorEvent.ERROR, onDBCreateTableError);
8. Execute! : sql.execute()