# Titanium.Database.DB

> The `Database` instance returned by [Titanium.Database.open](https://titaniumsdk.com/docs/sdk/Titanium.Database.md#open) or [Titanium.Database.install](https://titaniumsdk.com/docs/sdk/Titanium.Database.md#install).

- Source: https://titaniumsdk.com/docs/sdk/12.6.3/Titanium.Database.DB
- Titanium SDK version: 12.6.3
- Kind: proxy
- Platforms: Android, iPhone, iPad, macOS
- Since: Titanium SDK 0.1
- Extends: Titanium.Proxy
- Inheritance: Titanium.Proxy

## Properties

### file

- Type: Titanium.Filesystem.File
- Platforms: Android, iPhone, iPad, macOS
- Since: Titanium SDK Android 5.4.0, iPhone 2.0.0, iPad 2.0.0
- Permission: read-only

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

### lastInsertRowId

- Type: Number
- Platforms: Android, iPhone, iPad, macOS
- Permission: read-only

The identifier of the last populated row.

### name

- Type: String
- Platforms: Android, iPhone, iPad, macOS
- Permission: read-only

The name of the database.

### rowsAffected

- Type: Number
- Platforms: Android, iPhone, iPad, macOS
- Permission: read-only

The number of rows affected by the last query.

## Methods

### close

- Platforms: Android, iPhone, iPad, macOS

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](https://titaniumsdk.com/docs/sdk/Titanium.Database.ResultSet.md)
instances that exist.

### execute

- Platforms: Android, iPhone, iPad, macOS

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

Parameters:

- `sql` (String): SQL to execute. May include placeholders for parameter substitution.
- `vararg` (String | Array<String> | Object | Array<Object>, optional, repeatable): 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.

Returns: Titanium.Database.ResultSet

Example: Executing a Query

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

``` js
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](http://www.sqlite.org/lang_createtable.html#rowid)
column, which is a SQLite-specific column that stores the unique identifier for each row.

### executeAll

- Platforms: Android, iPhone, iPad, macOS
- Since: Titanium SDK 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](https://titaniumsdk.com/docs/sdk/BatchQueryError.md) that reports the failed index and partial results

Parameters:

- `queries` (Array<String>): Array of SQL queries to execute.

Returns: Array<Titanium.Database.ResultSet>

### executeAllAsync

- Platforms: Android, iPhone, iPad, macOS
- Since: Titanium SDK 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](https://titaniumsdk.com/docs/sdk/PossibleBatchQueryError.md) that reports the failed `index`, and a second argument with the partial `results`.

Parameters:

- `queries` (Array<String>): Array of SQL queries to execute.
- `callback` (Callback<PossibleBatchQueryError, Array<Titanium.Database.ResultSet>>, optional): Callback when query execution has completed.

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](https://titaniumsdk.com/docs/sdk/PossibleBatchQueryError.md).

### executeAsync

- Platforms: Android, iPhone, iPad, macOS
- Since: Titanium SDK 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:

- `query` (String): SQL query to execute. May include placeholders for parameter substitution.
- `vararg` (String | Array<String> | Object | Array<Object>, optional): 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>, optional): 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.

### remove

- Platforms: Android, iPhone, iPad, macOS

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 members

7 members are inherited. Each is documented on the type that declares it.

- [Titanium.Proxy](https://titaniumsdk.com/docs/sdk/12.6.3/Titanium.Proxy.md): `addEventListener`, `apiName`, `applyProperties`, `bubbleParent`, `fireEvent`, `lifecycleContainer`, `removeEventListener`

---

- This corpus documents Titanium SDK 13.4.1. Anything here that does not name a version describes 13.4.1.
- Titanium is maintained by TiDev. The Appcelerator toolchain is retired: there is no `appc` CLI, no Appcelerator Studio and no hosted build service. The supported CLI is `titanium` on npm, invoked as `ti`.

