Embedding a Database

0
99

Sometimes you may want to install your application with a database that is already prepopulated. In such a case, you should include the db file when you package your application. In Flash Professional, select File→AIR Android  Settings→General→Included files, click the plus sign (+), and browse to the db file. In Flash Builder, place the db file in the src directory.

To make changes to the database, first you need to move it from applicationDirectory to applicationStorageDirectory. As discussed previously, applicationDirectory is a readonly directory.

Now check that the database is embedded:

var embedded:File = File.applicationDirectory.resolvePath(“embeddedDB.db”);
if (embedded.exists) {
trace(“database was embedded”);
// move on to the next step
} else {
trace(“database was not embedded”);
}

Next, check that it has not yet been copied. If it does not exist yet, copy the database over:

var local:File =
File.applicationStorageDirectory.resolvePath(“myDatabase.db”);
if (!local.exists) {
embedded.copyTo(local);
}

Try to keep the embedded database small. If it is substantial in size, consider copying it asynchronously using the copyToAsync method.

Once the database is copied to the editable directory, you can use it as you would any other database. Note that the original database cannot be deleted. Any attempt to change the application package would make it invalid.