# Titanium.Database.ResultSet

> The ResultSet instance returned by [Titanium.Database.DB.execute](https://titaniumsdk.com/docs/sdk/Titanium.Database.DB.md#execute).

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

A result set represents the results returned by a database query.

The [rowCount](https://titaniumsdk.com/docs/sdk/Titanium.Database.ResultSet.md#rowCount) property identifies the number of
rows in the result set. The `ResultSet` object maintains an internal record of the 
current row. As shown in the example, you can use the 
[next](https://titaniumsdk.com/docs/sdk/Titanium.Database.ResultSet.md#next) method to iterate through the rows in the set.

Use the [field](https://titaniumsdk.com/docs/sdk/Titanium.Database.ResultSet.md#field) or
[fieldByName](https://titaniumsdk.com/docs/sdk/Titanium.Database.ResultSet.md#fieldByName) methods to query the fields for
the current row.

On the iOS platform, closing the database also closes the result set, that is,
you can only access the result set if the database is currently open.

## Example: Using ResultSet

The following code will create a database and execute SQL statements that will create a 
table, insert data into it, query the table and iterate through the returned
`ResultSet`.

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

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');
Ti.API.info('Row count: ' + rows.rowCount);

var fieldCount = rows.fieldCount;
Ti.API.info('Field count: ' + fieldCount);

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();
db.close();
```

Note that the above `SELECT` query contains the [rowid](http://www.sqlite.org/lang_createtable.html#rowid)
field, which contains an SQLite-specific unique identifier for each row.

## Properties

### fieldCount

- Type: Number
- Platforms: Android, iPhone, iPad, macOS
- Since: Titanium SDK 3.3.0
- Permission: read-only

The number of columns in this result set.

### rowCount

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

The number of rows in this result set.

### validRow

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

Indicates whether the current row is valid.

## Methods

### close

- Platforms: Android, iPhone, iPad, macOS

Closes this result set and release resources. Once closed, the result set must no longer 
be used.

### field

- Platforms: Android, iPhone, iPad, macOS

Retrieves the value for the specified field in the current row, 
and casts it to the specified type (String, Integer, Float or Double.)

All of the numeric types (Integer, Float or Double) are returned as JavaScript Number objects.  

If no `type` parameter is specified, the returned data type depends on the data in the column.  

* If the data in the column is TEXT, the data is returned as a String.
* If the data in the column is any kind of number, the data is returned as a Number.
* If the data in the column is a BLOB, the data is returned as a [Titanium.Blob](https://titaniumsdk.com/docs/sdk/Titanium.Blob.md) object.  

When a `type` is specified and the data cannot be converted to the specified type, an 
exception is thrown.  

Returns null if the value in the table is NULL.

Parameters:

- `index` (Number): A zero-based column index.
- `type` (Number, optional): Type to cast field value.

Returns: String | Number | Titanium.Blob - return type varies based on the underlying data and optional `type` argument. If BLOB, we return a Titanium.Blob. If TEXT, we return String. If any kind of number, we return a Number.

### fieldByName

- Platforms: Android, iPhone, iPad, macOS

Retrieves the value for the specified field in the current row, 
and casts it to the specified type (String, Integer, Float or Double.)

All of the numeric types (Integer, Float or Double) are returned as JavaScript Number objects.  

If no `type` parameter is specified, the returned data type depends on the data in the column.  

* If the data in the column is TEXT, the data is returned as a String.
* If the data in the column is any kind of number, the data is returned as a Number.
* If the data in the column is a BLOB, the data is returned as a [Titanium.Blob](https://titaniumsdk.com/docs/sdk/Titanium.Blob.md) object.  

When a `type` is specified and the data cannot be converted to the specified type, an 
exception is thrown.  

Returns null if the value in the table is NULL.

Parameters:

- `name` (String): A column name or alias used in the SQL query.
- `type` (Number, optional): Type to cast for the field value.

Returns: String | Number | Titanium.Blob - return type varies based on the underlying data and optional `type` argument. If BLOB, we return a Titanium.Blob. If TEXT, we return String. If any kind of number, we return a Number.

### fieldName

- Platforms: Android, iPhone, iPad, macOS

Returns the field name for the specified field index.

Parameters:

- `index` (Number): A zero-based column index for the field.

Returns: String

### getFieldName

- Platforms: Android

Returns the field name for the specified field index.

Parameters:

- `index` (Number): A zero-based column index for the field.

Returns: String

### isValidRow

- Platforms: Android, iPhone, iPad, macOS

Returns whether the current row is valid.

Returns: Boolean

### next

- Platforms: Android, iPhone, iPad, macOS

Advances to the next row in the result set and returns `true` if one exists, 
or `false` otherwise.

Returns: Boolean

## Inherited members

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

- [Titanium.Proxy](https://titaniumsdk.com/docs/sdk/12.7.0/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`.

