Titanium.Database.open

Function of Titanium.Database.
Platform Since
Android 0.1
iPhone 0.1
iPad 0.1
Mobile Web 1.8

Summary

Opens an SQLite database.

Description

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.

Code 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' &amp;&amp; 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

Arguments

Name Type Summary
dbName String

The dbname previously passed to Titanium.Database.install. On Android, an absolute path to the file, including one that is constructed with a Titanium.Filesystem constant, may be used.

Return Type