Parameters and results
Bind SQLite values, inspect rows and metadata, and handle query failures.
Bind parameters
SQLite assigns an index to each placeholder when it compiles a statement. An anonymous ? takes the next index, while ?NNN specifies an index. Named placeholders such as :name, @name, and $name get separate indices, and repeated uses of the same name share one index.
Pass a positional array to bind values by index, starting at SQLite index 1. Pass a record to bind by exact, case-sensitive placeholder name. Include the prefix. For example, :name and @name are different keys, and the key for ?5 is '?5'. If ?NNN refers to an index SQLite already named, use that index's first name.
const result = await db.executeAsync(
'SELECT :name AS name, :name AS repeated, @name AS other, ?5 AS rank',
{ ':name': 'Ada', '@name': 'Grace', '?5': 5 },
)A plain ? has no record key. To supply values for both anonymous and named placeholders in one statement, use a positional array ordered by SQLite parameter index. Unbound placeholders, null, and undefined all produce SQL NULL. Bindings start fresh on every execution, including when you reuse a prepared statement.
The public SQLiteValue type permits boolean, number, string, ArrayBuffer, null, and undefined. Keep table and column names in your SQL. Parameters only bind values. An unknown record key or extra array value throws or rejects with SqlExecutionError. Binding errors omit parameter values. These rules apply to synchronous, asynchronous, batch, and prepared statement execution.
Update typed wrappers and test doubles
The exported query function types now accept SQLiteQueryBindings, a positional array or a named record. Existing calls with arrays keep working. If you implement these types in a wrapper or test double, accept both forms. Narrow recorded parameters with Array.isArray(params) before indexing them as an array.
Read results
const name = 'Ada'
const age = 37
db.execute('INSERT INTO people (name, age) VALUES (?, ?)', [name, age])
const result = await db.executeAsync<{ name: string; age: number }>(
'SELECT name, age FROM people WHERE age >= ?',
[18],
)
for (const person of result.rows._array) {
console.log(person.name, person.age)
}results is an array of row objects keyed by column name. Its values retain the general SQLiteValue type. The connection adds rows._array with the same rows, rows.length, and rows.item(index). The row generic applies to rows._array and rows.item(); it does not validate values at runtime. item() returns undefined outside the array bounds. For a SELECT, use rows.length or results.length to count returned rows. rowsAffected uses SQLite's last change count, which can retain an earlier write's count after a SELECT; use it for INSERT, UPDATE, or DELETE. insertId exposes SQLite's last insert row ID when available and can also refer to an earlier statement.
SQLite INTEGER and REAL result values both arrive as JavaScript numbers. BLOB values arrive as ArrayBuffer; NULL values arrive as null, even if the parameter was undefined. A bound boolean is stored through SQLite's integer binding, so read it as a number and convert it in application code if needed. JavaScript numbers cannot represent every 64-bit SQLite integer exactly; choose a storage representation appropriate for identifiers that exceed the safe integer range.
Rows as arrays
A row object holds one value per column name, so when two result columns share a name, the later value replaces the earlier one. This happens in joins such as SELECT users.id, pets.id .... db.executeRaw() and db.executeRawAsync() take the same arguments as execute() and executeAsync() but return each row as an array of values in column order, so every column is kept.
const { columnNames, rows } = db.executeRaw(
'SELECT users.id, users.name, pets.id, pets.name FROM users LEFT JOIN pets ON pets.owner_id = users.id',
)
// columnNames: ['id', 'name', 'id', 'name']
// rows: [[1, 'Ada', 7, 'Rex'], [2, 'Grace', null, null]]The result has columnNames, rows, rowsAffected, and insertId. It has no rows._array container and no metadata. columnNames is filled even when the query returns no rows. Transactions and prepared statements have the same two methods. The exact shape is in NitroSQLiteRawQueryResult.
Column metadata
For a query with result columns, metadata maps column names to { name, type, index }. index is the zero-based result column position. The ColumnType values are BOOLEAN = 0, NUMBER = 1, INT64 = 2, TEXT = 3, ARRAY_BUFFER = 4, and NULL_VALUE = 5. The package exports ColumnType as a TypeScript type, not a runtime enum object. In the current native implementation, metadata.type is unreliable for detecting a column's declared SQL type. Use your schema for that decision. An expression without a declared type maps to NULL_VALUE, which does not mean the result itself is null. The exact shape is in NitroSQLiteQueryColumnMetadata.
const result = db.execute('SELECT name FROM people LIMIT 1')
const nameColumn = result.metadata?.name
if (nameColumn) {
console.log(nameColumn.name, nameColumn.index)
}Be especially careful with nullable columns and expressions when specifying a row type.
Errors
The connection's JavaScript helpers normalize database failures to NitroSQLiteError. Async methods reject; sync methods throw. An empty SQL string or one containing only comments also fails with the native category SqlExecutionError, because SQLite produces no statement to execute. Each query runs one statement, so SQL that contains another statement after the first fails with the same category instead of running only the first one; trailing whitespace, comments, and semicolons are allowed. Use batch operations to run several statements. Catch the error around the operation you can recover from:
import {
NitroSQLiteError,
NitroSQLiteExceptionType,
} from 'react-native-nitro-sqlite/errors'
try {
await db.executeAsync('SELECT * FROM missing_table')
} catch (error) {
if (
error instanceof NitroSQLiteError &&
error.type === NitroSQLiteExceptionType.SqlExecutionError
) {
console.error(error.message)
} else {
throw error
}
}NitroSQLiteError extends Error. NitroSQLiteError.fromError(error) returns an existing instance unchanged, copies an Error's message, cause, and stack, or uses a string as its message. For other values, it sets the message to 'Unknown error occurred' and keeps the original value as cause.
When a native NitroSQLiteException reaches a managed helper, error.type holds a NitroSQLiteExceptionType such as SqlExecutionError. It is undefined for JavaScript errors, including connection queue errors, and unrecognized native categories. These categories are not SQLite's numeric error codes. The native categories are UnknownError, DatabaseCannotBeOpened, EncryptionNotEnabled, DatabaseCannotBeDecrypted, DatabaseNotOpen, UnableToAttachToDatabase, SqlExecutionError, CouldNotLoadFile, and NoBatchCommandsProvided. See encrypt a database for the encryption errors. The current native DatabaseNotOpen helper emits UnableToAttachToDatabase, and a missing database file emits SqlExecutionError. Calls through NitroSQLite.native throw raw native errors.
The NitroSQLiteExceptionType constants expose these categories as runtime strings. The object and the TypeScript union share the same name, so existing type-only imports still work. Both the constants and NitroSQLiteError are available from the package root and react-native-nitro-sqlite/errors. The /errors entry point does not import React Native or initialize Nitro's native module, so shared classifiers and Node tests can use it without a native runtime. error.name remains NitroSQLiteError for every managed failure; use error.type to distinguish categories.
DatabaseCannotBeOpened covers several causes, including invalid paths, an already-open default connection, and filesystem failures. It does not by itself establish that the device is low on storage.
For exact types, see QueryResult, NitroSQLiteQueryResult, and NitroSQLiteQueryResultRows. The row generic narrows rows._array and rows.item(), but not the raw results array. Query results are plain JavaScript objects; see native access for results returned without the connection helper.