API Development

How to create and install a pre-populated database

Editor’s note: This blog post is a basic tutorial. The most up-to-date version is available in the wiki.

Using the Titanium.Database.install method allows you to install a database that is pre-populated and packaged with the application in the resources directory. When the Titanium.Database.install method is called, it will install that database, or if the database is already installed, it will simply open the database.

  • You can create a SQLite database in many ways – the easiest way might be to use the SQLite Manager plugin for Firefox located https://addons.mozilla.org/en-US/firefox/addon/5817.

This will allow you to create a new database, run SQL against it, and end up with a pre-populated database.

  • Place the resulting file in your resources folder and you will be able to use it with the install function:
var db = Titanium.Database.install('testdb.db','myDB');

This will install the testdb.db database as myDB.sql in the databases directory which is available using:

Ti.Filesystem.applicationSupportDirectory+'/database'.

From there you can now open that database using Ti.Database.open(‘myDB’);

Always remember to close your database and result set when you are done accessing them.