Titanium.Database.install

Function of Titanium.Database.
Platform Since
Android 0.1
iPhone 0.1
iPad 0.1

Summary

Installs an SQLite database to device's internal storage.

Description

Copies an SQLite database file to the device's internal storage (only) and creates a persistent name that is available for the lifetime of the app. On Android, if the source file does not exist, an empty database is created.

Returns a reference to the opened database. If the destination file already exists, behaves as Titanium.Database.open.

This method is primarily used for iOS.

With Android, as there is often minimal internal storage available, install may only be appropriate for small databases or for prototyping. When database files are to be stored on external storage (for example, SD Card), a combination of Titanium.Filesystem and Titanium.Database.open is required.

With Titanium 1.8.0.1 on iOS, the default database location changed in accordance with Apple's guidelines. If a database file already exists in the old location, install will automatically move it to the new location rather than copying the file from the path provided in the first argument.

Files stored in the Private Documents directory on iOS5 will be automatically backed up to iCloud and removed from the device in low storage situations. See How do I prevent files from being backed up to iCloud and iTunes? for details. To prevent this for database files, use the Titanium.Database.DB.file object with the Titanium.Filesystem.File.setRemoteBackup method.

Always close the database after use.

Code Examples

Install a Database (iOS)

A database, with a filename of mydb1 and located in the same directory as the the running script, is installed.

var db1 = Ti.Database.install('mydb1', 'mydb1Installed');

The file is copied to the default database location with a file extension of sql.

On simulator

  • /Users/user_name/Library/Application Support/iPhone Simulator/ios_version/Applications/apple_app_id/Library/Private Documents/mydb1Installed.sql (Titanium 1.8.0.1)
  • /Users/user_name/Library/Application Support/iPhone Simulator/ios_version/Applications/apple_app_id/Library/Application Support/database/mydb1Installed.sql (earlier versions)

On device

  • /Applications/apple_app_id/Library/Private Documents/mydb1Installed.sql (Titanium 1.8.0.1)
  • /Applications/apple_app_id/Library/Application Support/database/mydb1Installed.sql (earlier versions)

To prevent the database file being automatically backed up to iCloud, use setRemoteBackup.

db1.file.setRemoteBackup(false);

Install a Database to Internal Storage (Android)

A database, with a filename of mydb1 and located in the same directory as the the running script, is installed.

var db1 = Ti.Database.install('mydb1', 'mydb1Installed');

Unlike on iOS, no file extension is added. The file is opened in the following default database location, on both emulator and device.

  • file:///data/data/appID/databases/mydb1Installed

Install a Database to External Storage (Android)

The device is checked for the presence of external storage and a database, with a filename of mydb2 and located in the same directory as the the running script, is installed.

if (Ti.Platform.name === 'android' && Ti.Filesystem.isExternalStoragePresent()) {
  var db2 = Ti.Database.install('mydb2', Ti.Filesystem.externalStorageDirectory + 'path' + Ti.Filesystem.separator + 'to' + Ti.Filesystem.separator + 'mydb2Installed');
}

Unlike on iOS, no file extension is added. The file is copied to the absolute path provided.

  • file:///sdcard/path/to/mydb2Installed

Arguments

Name Type Summary
path String

Path and filename of the database file to copy to internal storage. File location is relative to the script's context unless an absolute path, such as one constructed with a Titanium.Filesystem constant, is used.

dbName String

Destination filename, which will subsequently be passed to Titanium.Database.open.

Return Type