API Docs

A prepared statement, as Node's StatementSync. Made by DatabaseSync.prepare(); it can't be constructed directly.

Parameters are bound the same way for run(), get(), all() and iterate(): if the first argument is an object (other than a typed array), its properties are bound to named parameters (:name, @name or $name), and any remaining arguments are bound, in order, to anonymous (?) parameters. Values convert as in Node:

JavaScript SQLite
null, undefined NULL
number REAL (stored as INTEGER in columns with INTEGER affinity)
boolean INTEGER 1 or 0
bigint INTEGER
string TEXT
typed array (e.g. Uint8Array) BLOB

Results come back as null, numbers (or BigInts, see setReadBigInts()), strings and Uint8Arrays. An INTEGER too large to be a JS number throws, unless readBigInts is set.

Properties

expandedSQL

string
The statement's SQL, with each parameter replaced by the value most recently bound to it.

sourceSQL

string
The statement's SQL, as passed to `prepare()`.

Methods

run(...params) -> { changes: number | bigint, lastInsertRowid: number | bigint }

Runs the statement, and returns the number of rows it changed and the ID of the last row inserted.
run(...params) -> { changes: number | bigint, lastInsertRowid: number | bigint }
Name Type Description
...params any[] Named parameters (an object) and/or anonymous parameters
{ changes: number | bigint, lastInsertRowid: number | bigint }
The rows changed and the last inserted row ID. These are `BigInt`s if `readBigInts` is set.
const insert = db.prepare('INSERT INTO notes (body) VALUES (:body)')
const { lastInsertRowid } = insert.run({ body: 'Buy milk' })

get(...params) -> any

Runs the statement and returns its first row.
get(...params) -> any
Name Type Description
...params any[] Named parameters (an object) and/or anonymous parameters
any
The first row, as an object keyed by column name (or an array, if `returnArrays` is set), or `undefined` if there are no rows
const note = db.prepare('SELECT * FROM notes WHERE id = ?').get(1)
if (note) console.log(note.body)

all(...params) -> any[]

Runs the statement and returns all of its rows.
all(...params) -> any[]
Name Type Description
...params any[] Named parameters (an object) and/or anonymous parameters
any[]
The rows, each an object keyed by column name (or an array, if `returnArrays` is set)
for (const note of db.prepare('SELECT * FROM notes').all()) {
    console.log(note.id + ': ' + note.body)
}

iterate(...params) -> IterableIterator

Runs the statement and returns an iterator over its rows, which are read as the iterator is advanced. Running the statement again invalidates the iterator.
iterate(...params) -> IterableIterator
Name Type Description
...params any[] Named parameters (an object) and/or anonymous parameters
IterableIterator
An iterator over the rows
for (const note of db.prepare('SELECT * FROM notes').iterate()) {
    if (note.body.includes('milk')) break
}

columns() -> Array<{ column: string | null, database: string | null, name: string, table: string | null, type: string | null }>

Describes the columns the statement returns.
columns() -> Array<{ column: string | null, database: string | null, name: string, table: string | null, type: string | null }>
Array<{ column: string | null, database: string | null, name: string, table: string | null, type: string | null }>
One object per column: its name in the result (`name`), and, for a column read directly from a table, the original column name, database, table and declared type
const names = db.prepare('SELECT * FROM notes').columns().map(c => c.name)

setReadBigInts(enabled) -> None

Sets whether INTEGER values are read as `BigInt`s, including `run()`'s `changes` and `lastInsertRowid`.
setReadBigInts(enabled) -> None
Name Type Description
enabled JSValue Whether to read `BigInt`s
None
stmt.setReadBigInts(true)

setReturnArrays(enabled) -> None

Sets whether rows are returned as arrays of values instead of objects.
setReturnArrays(enabled) -> None
Name Type Description
enabled JSValue Whether to return arrays
None
stmt.setReturnArrays(true)

setAllowBareNamedParameters(enabled) -> None

Sets whether named parameters may be given without their prefix character (`{ id: 1 }` for `:id`).
setAllowBareNamedParameters(enabled) -> None
Name Type Description
enabled JSValue Whether to allow bare names
None
stmt.setAllowBareNamedParameters(false)

setAllowUnknownNamedParameters(enabled) -> None

Sets whether named parameters the statement doesn't use are ignored, rather than throwing.
setAllowUnknownNamedParameters(enabled) -> None
Name Type Description
enabled JSValue Whether to ignore unknown names
None
stmt.setAllowUnknownNamedParameters(true)