You are reading 12.6.4. See this page in 13.4.1.
Titanium.Database.DB
Extends Titanium.Proxy
The Database instance returned by Titanium.Database.open or Titanium.Database.install.
Properties 7
Inherited from Titanium.Proxy
The name of the API that this proxy corresponds to.
Inherited from Titanium.Proxy
Indicates if the proxy will bubble an event to its parent.
Default: true
A File object representing the file where this database is stored. Must only be used for
setting file properties.
The identifier of the last populated row.
Inherited from Titanium.Proxy
The Window or TabGroup whose Activity lifecycle should be triggered on the proxy.
The name of the database.
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 | Type | Description |
|---|---|---|
| name | String | Name of the event. |
| callback | Callback<Titanium.Event> | Callback function to invoke when the event is fired. 4 fields
|
applyProperties
since 3.0.0Inherited from Titanium.Proxy
Applies the properties to the proxy.
| Parameters | Type | Description |
|---|---|---|
| props | Dictionary | A dictionary of properties to apply. |
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 | Type | Description |
|---|---|---|
| sql | String | 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 |
executeAll
since 8.1.0Synchronously 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 | Type | Description |
|---|---|---|
| queries | Array<String> | Array of SQL queries to execute. |
executeAllAsync
since 8.1.0Asynchronously 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 | Type | Description |
|---|---|---|
| queries | Array<String> | Array of SQL queries to execute. |
| callback? | Callback<PossibleBatchQueryError, Array<Titanium.Database.ResultSet>> | Callback when query execution has completed. 1 field
|
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.0Asynchronously 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 | Type | Description |
|---|---|---|
| query | String | 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 |
| callback? | Callback<Object, Titanium.Database.ResultSet> | Callback when query execution has completed. |
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 | Type | Description |
|---|---|---|
| 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. |
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 | Type | Description |
|---|---|---|
| name | String | Name of the event. |
| callback | Callback<Titanium.Event> | Callback function to remove. Must be the same function passed to 4 fields
|