Work with a local database

For security reasons AIR allow the run-time to modify files that are not stored in the application directory, for this reason we are used to ship our application with a default database and then make a copy of it in the application storage directory.

In order to make our life easier we defined a LocalData class that is able to perform this copy the data base in a specific path and keep live a connection to this file.
The steps to follow for the initialization of the data base connection are

  • Get an instance of the class
  • Specify the source file and the location to which the file has to be copied
  • Initialize the database
// Get the instance of the class
localData = LocalData.getInstance();

// Destination and source of the default database
var destination:File = File.applicationStorageDirectory.resolvePath('data/fototaxi_def.sqlite')
var source:File = File.applicationDirectory.resolvePath('fototaxi_new.db')
						
// Initialization of the database
localData.initializedDataBase(source, destination);

When the operation is completed the LocalData class dispatches an event and create a connection to the database that you can recover with the getConnectionByName(name:String):SQLConnection method.
In order to be sure that the application will work as expected it's strongly recommended to define two listeners in order to handle the connection success and fault, remember that this events are always fired also if the database file was already copied to the destination directory (is the LocalData class that internally avoid to override an exiting file if you don't set the erase parameter to true in the initializeDataBase() method.

// Define the listener for the complete initialization of the connection to the database
localData.addEventListener(LocalData.INITIALIZATION_COMPLETED, onDefaultConnection);
localData.addEventListener(LocalData.INITIALIZATION_NOT_COMPLETED, onDefaultConnectionFault);

In order to handle failures and successful data manipulation is strongly recomended that you define also the listener for these events in the initialization steps of the application

// Define the listener invoked each time a query is successfuly run
localData.addEventListener(DBEvent.SUCCESS_QUERY, onDBDataRecovered);
			
// Define the listener invoked each time a query fails
localData.addEventListener(DBEvent.FAILED, onDBDataFailed);

TODO define the helper for database queries

1.1.2-pro © 2008-2009 agile42 all rights reserved (this page was served in: 0.47000 sec.)