Skip to content
Titanium SDK
Version13.4.0

You are reading 13.4.0. See this page in 13.4.1.

Titanium.Database

moduleAndroidiPhoneiPadmacOSsince 0.1

Extends Titanium.ModuleTitanium.Proxy

The top-level Database module, used for creating and accessing the in-application SQLite database.

Properties 7

apiName

Stringsince Android 3.2.0, iPhone 3.2.0, iPad 3.2.0, macOS 9.2.0read-only

Inherited from Titanium.Proxy

The name of the API that this proxy corresponds to.

bubbleParent

Booleansince Android 3.0.0, iPhone 3.0.0, iPad 3.0.0, macOS 9.2.0

Inherited from Titanium.Proxy

Indicates if the proxy will bubble an event to its parent.

Default: true

FIELD_TYPE_DOUBLE

Numberread-only

Constant for requesting a column's value returned in double form.

FIELD_TYPE_FLOAT

Numberread-only

Constant for requesting a column's value returned in float form.

FIELD_TYPE_INT

Numberread-only

Constant for requesting a column's value returned in integer form.

FIELD_TYPE_STRING

Numberread-only

Constant for requesting a column's value returned in string form.

Methods 6

Inherited from Titanium.Proxy

Adds the specified callback as an event listener for the named event.

Parameters
ParametersTypeDescription
nameString

Name of the event.

callbackCallback<Titanium.Event>

Callback function to invoke when the event is fired.

4 fields
bubbles Boolean

True if the event will try to bubble up if possible.

cancelBubble Boolean

Set to true to stop the event from bubbling.

source Object

Source object that fired the event.

type String

Name of the event fired.

Inherited from Titanium.Proxy

Fires a synthesized event to any registered listeners.

Parameters
ParametersTypeDescription
nameString

Name of the event.

event?Dictionary

A dictionary of keys and values to add to the Titanium.Event object sent to the listeners.

Installs an SQLite database to device's internal storage.

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 iOS 5 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? for details. To prevent this for database files, use the Titanium.Database.DB.file object with the Titanium.Filesystem.File.remoteBackup property.

Always close the database after use.

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 remoteBackup.

db1.file.remoteBackup = 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
Parameters
ParametersTypeDescription
pathString

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.

dbNameString

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

Opens an SQLite database.

Opens an SQLite database and returns a reference to it. If the database does not exist, creates an empty database file and returns a reference to this opened database.

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, open will automatically move it to the new location.

Always close the database after use.

Open a Database from Internal Storage (iOS)

A database, with a persistent name of mydb1Installed and located in the default database location on internal storage, is opened.

var db = Ti.Database.open('mydb1Installed');

A file extension of sql is added, and the file is opened from the following location.

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)

Open a Database on Internal Storage (Android)

A database, with a persistent name of mydb1Installed and located in the default database location on internal storage, is opened.

var db1 = Ti.Database.open('mydb1Installed');

Unlike on iOS, no file extension is added. The file is opened in the following location.

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

Open a Database on External Storage (Android)

A database, with a filename of mydb2Installed and located at the absolute path provided, is opened.

if (Ti.Platform.name === 'android' && Ti.Filesystem.isExternalStoragePresent()) {
  var db2 = Ti.Database.open(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, as follows.

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

Open a Database in the shared directory of an application group (iOS)

Opens a Database in a shared directory

var suiteDir = Ti.Filesystem.directoryForSuite('group.appc.Sharing');
if (!suiteDir) {
    logInApp('Suite Directory not found, check Entitlements.');
    return;
}
var path = suiteDir + 'TestDB';
var db = Ti.Database.open(path);
db.close();
Parameters
ParametersTypeDescription
dbNameString

The dbname previously passed to Titanium.Database.install. An absolute path to the file, including one that is constructed with a Titanium.Filesystem constant or Titanium.Filesystem.directoryForSuite method, may be used.

Inherited from Titanium.Proxy

Removes the specified callback as an event listener for the named event.

Parameters
ParametersTypeDescription
nameString

Name of the event.

callbackCallback<Titanium.Event>

Callback function to remove. Must be the same function passed to addEventListener.

4 fields
bubbles Boolean

True if the event will try to bubble up if possible.

cancelBubble Boolean

Set to true to stop the event from bubbling.

source Object

Source object that fired the event.

type String

Name of the event fired.