Skip to content
Titanium SDK
Version13.0.0

You are reading 13.0.0. See this page in 13.4.1.

Titanium.Database.DB

proxyAndroidiPhoneiPadmacOSsince 0.1

Extends Titanium.Proxy

The Database instance returned by Titanium.Database.open or Titanium.Database.install.

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

file

Titanium.Filesystem.Filesince Android 5.4.0, iPhone 2.0.0, iPad 2.0.0read-only

A File object representing the file where this database is stored. Must only be used for setting file properties.

name

Stringread-only

The name of the database.

rowsAffected

Numberread-only

The number of rows affected by the last query.

Methods 10

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.

Closes the database and releases resources from memory. Once closed, this instance is no longer valid and should not be used. On iOS, also closes all Titanium.Database.ResultSet instances that exist.

Executes an SQL statement against the database and returns a ResultSet.

Executing a Query

The following code will install a database using Titanium.Database.install and execute SQL statements that will create a table, insert data and query the table.

var db = Ti.Database.install('mydb1', 'mydb1Installed');
db.execute('DELETE FROM people');
db.execute('CREATE TABLE IF NOT EXISTS people (name TEXT, phone_number TEXT, city TEXT)');

var thisName = 'Arthur';
var thisPhoneNo = '1-617-000-0000';
var thisCity = 'Mountain View';
db.execute('INSERT INTO people (name, phone_number, city) VALUES (?, ?, ?)', thisName, thisPhoneNo, thisCity);

var personArray = ['Paul','020 7000 0000', 'London'];
db.execute('INSERT INTO people (name, phone_number, city) VALUES (?, ?, ?)', personArray);

var rows = db.execute('SELECT rowid,name,phone_number,city FROM people');
db.close();

while (rows.isValidRow()) {
  Ti.API.info('Person ---> ROWID: ' + rows.fieldByName('rowid') + ', name:' + rows.field(1) + ', phone_number: ' + rows.fieldByName('phone_number') + ', city: ' + rows.field(3));
  rows.next();
}
rows.close();

Note that the above SELECT query contains the rowid column, which is a SQLite-specific column that stores the unique identifier for each row.

Parameters
ParametersTypeDescription
sqlString

SQL to execute. May include placeholders for parameter substitution.

vararg?String | Array<String> | Object | Array<Object>

Either a variable ordered list of zero or more values, or an array of values, to be substituted with the respective ? placeholder of the query.

executeAll

since 8.1.0

Synchronously executes an array of SQL statements against the database and returns an array of ResultSet. On failure, this will throw an Error that reports the failed index and partial results

Parameters
ParametersTypeDescription
queriesArray<String>

Array of SQL queries to execute.

executeAllAsync

since 8.1.0

Asynchronously executes an array of SQL statements against the database and fires a callback with a possible Error, and an array of ResultSet. On failure, this will call the callback with an Error that reports the failed index, and a second argument with the partial results.

Parameters
ParametersTypeDescription
queriesArray<String>

Array of SQL queries to execute.

callback?Callback<PossibleBatchQueryError, Array<Titanium.Database.ResultSet>>

Callback when query execution has completed.

1 field
index Number

Index of the failed query

Returns Promise<Array<Titanium.Database.ResultSet>>

On SDK 10+, this method will return a Promise whose resolved value is equivalent to that passed to the optional callback argument. If the Promise is rejected, the Error will have custom properties indicating the index of the failed query, and the partial results up to that failure. See PossibleBatchQueryError.

executeAsync

since 8.1.0

Asynchronously executes an SQL statement against the database and fires a callback with a possible Error argument, and a second argument holding a possible ResultSet.

Parameters
ParametersTypeDescription
queryString

SQL query to execute. May include placeholders for parameter substitution.

vararg?String | Array<String> | Object | Array<Object>

Either a variable ordered list of zero or more values, or an array of values, to be substituted with the respective ? placeholder of the query.

callback?Callback<Object, Titanium.Database.ResultSet>

Callback when query execution has completed.

Returns Promise<Titanium.Database.ResultSet>

On SDK 10+, this method will return a Promise whose resolved value is equivalent to that passed to the optional callback argument.

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.

Removes the database files for this instance from disk. WARNING: this is a destructive operation and cannot be reversed. All data in the database will be lost; use with caution.

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.