Titanium.Database.DB.execute

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

Summary

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

Code Examples

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.

Arguments

Name Type Summary
sql String

SQL to execute. May include placeholders for parameter substitution.

vararg String or Array<String> or Object or 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. Optional.

Return Type