114 | Chapter 4: AIR Mini-Cookbook
Currently, when working with a local database from AIR, all
transactions are asynchronous. This configuration allows the
user interface to continue to respond while the database is
processing in the background. To monitor database activity,
an application must create and register for the events in
which it is interested.
Connecting to a Database
Problem
You need to connect to a local database prior to working
with the schema or altering data.
Solution
Creating and connecting to a database can be accomplished
using the singleSQLConnection.open() method.
Discussion
SQLite stores all database information in a single file on disk.
This means that before an application can access a database,
it must first have a reference to the file. Asingle application
might choose to access any number of database files. Data-
bases are managed through theSQLConnectiondatatype in the
AIR API.
Obtaining a reference to the database file can be done
through theFile.resolve()method, which takes a single
argument: the name of the file that will be referenced. Files
that do not yet exist can have a reference, and theFile.
exists property returns a Boolean to determine that file’s
presence on disk:
var db = new air.SQLConnection();
var file =
air.File.applicationStorageDirectory.resolve( "mycrm.db" );
The extension to the database file is not specific and can be
named as necessary for the application.