Titanium.Database
> Titanium.Database

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

  • 0.8
  • 0.8
  • 0.8
Defined By

Properties

Titanium.Database
FIELD_TYPE_DOUBLE : Numberreadonly

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

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

Titanium.Database
FIELD_TYPE_FLOAT : Numberreadonly

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

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

Titanium.Database
FIELD_TYPE_INT : Numberreadonly

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

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

Titanium.Database
FIELD_TYPE_STRING : Numberreadonly

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

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

apiName : Stringreadonly

The name of the API that this proxy corresponds to.

The name of the API that this proxy corresponds to.

The value of this property is the fully qualified name of the API. For example, Button returns Ti.UI.Button.

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

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

Some proxies (most commonly views) have a relationship to other proxies, often established by the add() method. For example, for a button added to a window, a click event on the button would bubble up to the window. Other common parents are table sections to their rows, table views to their sections, and scrollable views to their views. Set this property to false to disable the bubbling to the proxy's parent.

Default: true

  • 3.0.0
  • 3.0.0
  • 3.0.0

The Window or TabGroup whose Activity lifecycle should be triggered on the proxy.

The Window or TabGroup whose Activity lifecycle should be triggered on the proxy.

If this property is set to a Window or TabGroup, then the corresponding Activity lifecycle event callbacks will also be called on the proxy. Proxies that require the activity lifecycle will need this property set to the appropriate containing Window or TabGroup.

  • 3.6.0
Defined By

Methods

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

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

Parameters

  • name : String

    Name of the event.

  • callback : Callback<Object>

    Callback function to invoke when the event is fired.

Returns

  • void
Applies the properties to the proxy. ...

Applies the properties to the proxy.

Properties are supplied as a dictionary. Each key-value pair in the object is applied to the proxy such that myproxy[key] = value.

  • 3.0.0
  • 3.0.0
  • 3.0.0

Parameters

  • props : Dictionary

    A dictionary of properties to apply.

Returns

  • void
Fires a synthesized event to any registered listeners. ...

Fires a synthesized event to any registered listeners.

Parameters

  • name : String

    Name of the event.

  • event : Dictionary

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

Returns

  • void
Gets the value of the apiName property. ...

Gets the value of the apiName property.

  • 3.2.0
  • 3.2.0
  • 3.2.0

Returns

  • String
Gets the value of the bubbleParent property. ...

Gets the value of the bubbleParent property.

  • 3.0.0
  • 3.0.0
  • 3.0.0

Returns

  • Boolean
Gets the value of the lifecycleContainer property. ...

Gets the value of the lifecycleContainer property.

  • 3.6.0

Returns

Titanium.Database
( path, dbName ) : Titanium.Database.DB
Installs an SQLite database to device's internal storage. ...

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

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

Parameters

  • 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 or absolute path, which will subsequently be passed to open.

Returns

Titanium.Database
( dbName ) : Titanium.Database.DB
Opens an SQLite database. ...

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.

Examples

Open a Database from Internal Storage (iOS)

A database, with a persistant 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 persistant 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

Returns

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

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

Multiple listeners can be registered for the same event, so the callback parameter is used to determine which listener to remove.

When adding a listener, you must save a reference to the callback function in order to remove the listener later:

var listener = function() { Ti.API.info("Event listener called."); }
window.addEventListener('click', listener);

To remove the listener, pass in a reference to the callback function:

window.removeEventListener('click', listener);

Parameters

  • name : String

    Name of the event.

  • callback : Callback<Object>

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

Returns

  • void
Sets the value of the bubbleParent property. ...

Sets the value of the bubbleParent property.

  • 3.0.0
  • 3.0.0
  • 3.0.0

Parameters

  • bubbleParent : Boolean

    New value for the property.

Returns

  • void
Sets the value of the lifecycleContainer property. ...

Sets the value of the lifecycleContainer property.

  • 3.6.0

Parameters

Returns

  • void