Learn to use SQLite in real-world Flex mobile apps

Last week, Adobe announced the public betas of Flex “Hero” and Flash Builder “Burrito.” You can use these tools to begin creating your own mobile applications that run Flash, AIR, and Flex content, and I recommend you check them out here and here. If you’re looking for the next step in getting started with a real-world Flex app, however, you may be interested in the following:

I created an open-source application called SurveyApe, which should help you get started in the process of creating robust, real-world Flex mobile applications. It’s available on Adobe Labs:

Update: New URL: http://www.adobe.com/devnet/flex/samples/survey-ape-application.html 05/16/2011

SurveyApe utilizes SQLite, the database framework that’s available in Adobe AIR. If you’re new to web or application development, SQLite is a relational database that allows you to store and access large amounts of data using fast, structured queries. If your mobile applications collect medium to large amounts of user data, or if they pull data sets from the Internet, chances are you’re going to want to store in in a database. The use-cases where you want to use a relational database over your own binary file format are numerous. You’ll find databases useful for games, data-driven applications, Twitter and Facebook-style applications that pull down status information, etc. You can use the design patterns I’ve created in SurveyApe to help create these sorts of applications in Flex “Hero” and AIR for Android.

Ostensibly, SurveyApe conducts a survey consisting of a predetermined set of questions, allowing multiple users to use your mobile device and answer the questions that are provided in a sample database. The survey-giver can then review statistics and summary information about the responses, including question breakdowns and some simple demographic data. It’s really useful, however, to demonstrate how to create your own asynchronous database access objects, and the code includes some best-practices for preventing memory leaks and ensuring sane access patterns.

When you first import SurveyApe into Flash Builder (or your IDE of choice), you should open the src/database/Database.as file. This code contains all the SQL that the program can execute, as well as access methods that are accessible from every instance of the Database class (you’ll only want one instance per database). You’ll see the asynchronous pattern I recommend using when you view the various methods in this class.

For example:

 * Gets the list of surveys
 * @param args Expects element 0 to be a DatabaseResponder.
public function getSurveys(args:Array):void
	if ( args[0] is DatabaseResponder )
		var sqlWrapper:SQLWrapper = this.sqlStatementFactory.newInstance(args[0], GET_SURVEYS);


The sqlStatementFactory is an instance of SQLStatementFactory…a class I created to ensure that I have one function responsible for generating event handlers, and–in so doing–I can be assured that every call to the database results in event handlers that are garbage collected. (If you have event handlers registered on an object and you set the object to null, the event handlers will remain in memory, creating a leak.)

Note that I’m passing parameters via weak typing in an Array. This was fast and easy to set up, but you may want to create your own DatabaseArgs Object, which will allow you to access arguments using key value pairs, which is a more robust, safer way to pass arguments.

SurveyApe is too large to cover everything in one blog post, so stay tuned to learn about some more Flex “Hero” and AIR features I utilize…including how to take images and video using using the native CameraUI.

Download survey ape 0.4
Update: Download Survey Ape 0.4