The sample application discussed in this article is intentionally simple. It creates a database in the computer's memory as well as a table in that database, and adds some data to the database. Clicking the Load data button then retrieves the data and displays it on the screen (see Figure 1). Other than loading data when the button is clicked and displaying it on the screen, there is no additional user interaction available. This is intentional, to focus the application entirely on the database operations. This sample application demonstrates the following Adobe AIR features:

Connecting to a local SQL database using asynchronous execution mode

Creating and executing SQL statements asynchronously:

Creating a table in the database

Inserting data into the database table

Retrieving data from the database table and displaying that data in a Flex DataGrid component

Figure 1. This sample application enables you to load data from a database.

Note: This is a sample application provided, as is, for instructional purposes.

This sample application includes the following files:

SimpleDBExampleFlex.mxml: The main application file in MXML for Flex; includes the code discussed in this article

SimpleDBExampleFlex-app.xml: The AIR application descriptor file

Sample AIR icon files

Important

The application descriptor file, SimpleDBExampleFlex-app.xml, uses the AIR 1.0 namespace. If you are compiling the application using Flex Builder 3.0.2 or Flex SDK 3.2 or later, edit the file to use the AIR 1.5 namespace:

xmlns="http://ns.adobe.com/air/application/1.5"

Otherwise, you will not be able to test or package the application.

Understanding the code

Note: This article does not describe all of the Flex components used in the MXML code for the file. For more information, see the Flex 3 Language Reference.

Connecting to a local SQL database

The init() method is called when the application finishes loading. Within this method, a SQLConnection instance name conn is created. (The variable conn is declared outside the method so that it is available to all the code in the application.) This SQLConnection object establishes the connection to a database, and is used by other objects to perform operations on that specific database. Once the SQLConnection instance is created, event listeners are registered with it to be called when the database connection is opened (or the openAsync() operation fails), and the openAsync() method is called to open the connection to the database in asynchronous execution mode.

In this case null is passed as an argument to the openAsync() method, indicating that the runtime creates a database in the computer's memory rather than in a disk location. Alternatively, you could specify a file location (using a File instance). The runtime would then open the database file at that location (creating it first if it doesn't exist). The code to do that would look like this:

File.applicationStorageDirectory points to the AIR application store directory, which is uniquely defined for each AIR application.

Assuming the openAsync() operation succeeds and the database connection opens, the openSuccess() method is called. That method simply performs the clean-up operation of removing the event listener registrations, and calls the createTable() method that does the work of creating a table in the database.

Creating a table in the database

The createTable() method uses a SQLStatement instance to execute a SQL command against the database that was opened in the init() method. The specific SQL command creates a table in the database named "employees," with four columns. Here is a breakdown of the code and what it does:

Creates a SQLStatement instance named createStmt:

createStmt = new SQLStatement();

Specifies that the statement will execute on the database that's connected through the SQLConnection instance conn:

createStmt.sqlConnection = conn;

Defines the SQL statement text to create a database table. The table is named "employees." It has four columns: "empId," "firstName," "lastName," and "salary."

Assuming that the statement runs successfully, the "employees" table is created and the createResult() method is called. That method removes the registered listeners and calls the addData() method to perform the next step in the process, adding data into the newly created table.

Inserting data into the database table

Like the createTable() method, the addData() method creates a SQLStatement, in this case to insert a row of data into the "employees" table in the database. The application inserts two rows of data, using two different SQLStatement instances (insertStmt and insertStmt2):

Note that because the second statement execution doesn't depend on the result of the first one, the second SQLStatement instance is created, and its execute() method is called immediately after the first instance's execute() method is called. (As opposed to waiting for the result event of the first INSERT statement before executing the second one.) The runtime queues up these two statements, executing the second one immediately after the first one completes.

The only complicating factor is that the code needs to determine that both statements have completed before it moves on to retrieve data from the database. To do this, in the insertResult() method (which is called when either SQLStatement's result event is triggered) the application determines which statement finished executing, then checks whether both statements have finished executing. If they have, the status bar text (status) is updated to read "Ready to load data" and the application is ready to retrieve the data from the database and display it on the screen:

Like creating a table and inserting data into the table, retrieving data from a table is carried out by creating a SQLStatement instance with a SQL SELECT statement as the SQLStatement instance's text property. The following code, from the getData() method, creates and executes the SELECT statement that retrieves all the rows from the "employees" table:

As specified in the code, when the SELECT statement finishes executing the selectResult() method is called. In selectResult(), the result data that is retrieved by the SELECT statement is accessed by calling the SQLStatement instance's getResult() method. Calling getResult() returns a SQLResult instance that is stored in the variable result; the actual result rows are contained in an array in its data property. The results are displayed in the Flex DataGrid control named resultsGrid by setting it (that is, the result.data property) as the resultsGrid data grid's dataProvider property: