NitroSQLite
Guides

Parameters and results

Bind SQLite values, inspect rows and metadata, and handle query failures.

Pass values as the second argument to db.execute() or db.executeAsync(), then read the returned rows, rowsAffected, and insertId.

In NitroSQLite, pass positional values in an array for ? placeholders. The public SQLiteValue type permits boolean, number, string, ArrayBuffer, null, and undefined. Both null and undefined bind as SQL NULL. Keep table and column names in your own SQL; parameters bind values, not identifiers.

SQLite leaves omitted placeholders as SQL NULL, so NitroSQLite does not require an exact parameter count. Extra values and other binding failures throw or reject with the one-based parameter index and SQLite error code and text. The error does not include parameter values. This behavior applies to synchronous, asynchronous, batch, and prepared statement execution.

For repeated executions of the same SQL, prepare the statement once and pass a new parameter array on each run.

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.

SQLite uses the alias in AS as the result column name. Each column in a native NitroSQLite result is an own enumerable property of the row object. An alias such as __proto__ holds the column value, and the row still inherits from Object.prototype.

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 NativeRawQueryResult.

Read columns by position

SQLite can return several columns with the same name. Object rows have one property per name. For a join or a driver that needs each value in selection order, use executeRaw() or executeRawAsync() on the connection. The columnNames array keeps repeated names. Each entry in rows holds the corresponding values in order.

const result = await db.executeRawAsync('SELECT ? AS id, ? AS id', [1, 2])
// result.columnNames is ['id', 'id']
// result.rows is [[1, 2]]

Transactions and prepared statements also support raw results. The Drizzle driver uses them to map joined columns back to their tables.

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 QueryColumnMetadata.

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 example imports react-native-nitro-sqlite/errors for React Native. NitroUI apps use nitro-sqlite/errors. Both packages export the same error helpers. See supported platforms.

The connection's JavaScript helpers normalize database failures to SQLiteError. 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 {
  SQLiteError,
  SQLiteExceptionType,
} from 'react-native-nitro-sqlite/errors'

try {
  await db.executeAsync('SELECT * FROM missing_table')
} catch (error) {
  if (
    error instanceof SQLiteError &&
    error.type === SQLiteExceptionType.SqlExecutionError
  ) {
    console.error(error.message)
  } else {
    throw error
  }
}

SQLiteError extends Error. SQLiteError.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 SQLiteExceptionType 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 SQLiteExceptionType 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 SQLiteError are available from each package's root and /errors entry point. The /errors entry points do not initialize the native module, so shared classifiers and Node tests can use them without a native runtime. error.name is SQLiteError. Use error instanceof SQLiteError to identify a managed failure and error.type to distinguish its category. The deprecated NitroSQLiteError export refers to the same constructor, so existing instanceof NitroSQLiteError checks also work.

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, NativeQueryResult, and QueryResultRows. 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.

On this page