StatementSync
TypeA 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.
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.
Declaration
run(...params) -> { changes: number | bigint, lastInsertRowid: number | bigint }
Parameters
| Name | Type | Description |
|---|---|---|
| ...params | any[] | Named parameters (an object) and/or anonymous parameters |
Returns
{ changes: number | bigint, lastInsertRowid: number | bigint }
The rows changed and the last inserted row ID. These are `BigInt`s if `readBigInts` is set.
Example
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.
Declaration
get(...params) -> any
Parameters
| Name | Type | Description |
|---|---|---|
| ...params | any[] | Named parameters (an object) and/or anonymous parameters |
Returns
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
Example
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.
Declaration
all(...params) -> any[]
Parameters
| Name | Type | Description |
|---|---|---|
| ...params | any[] | Named parameters (an object) and/or anonymous parameters |
Returns
any[]
The rows, each an object keyed by column name (or an array, if `returnArrays` is set)
Example
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.
Declaration
iterate(...params) -> IterableIterator
Parameters
| Name | Type | Description |
|---|---|---|
| ...params | any[] | Named parameters (an object) and/or anonymous parameters |
Returns
IterableIterator
An iterator over the rows
Example
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.
Declaration
columns() -> Array<{ column: string | null, database: string | null, name: string, table: string | null, type: string | null }>
Returns
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
Example
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`.
Declaration
setReadBigInts(enabled) -> None
Parameters
| Name | Type | Description |
|---|---|---|
| enabled | JSValue | Whether to read `BigInt`s |
Returns
None
Example
stmt.setReadBigInts(true)
setReturnArrays(enabled) -> None
Sets whether rows are returned as arrays of values instead of objects.
Declaration
setReturnArrays(enabled) -> None
Parameters
| Name | Type | Description |
|---|---|---|
| enabled | JSValue | Whether to return arrays |
Returns
None
Example
stmt.setReturnArrays(true)
setAllowBareNamedParameters(enabled) -> None
Sets whether named parameters may be given without their prefix character (`{ id: 1 }`
for `:id`).
Declaration
setAllowBareNamedParameters(enabled) -> None
Parameters
| Name | Type | Description |
|---|---|---|
| enabled | JSValue | Whether to allow bare names |
Returns
None
Example
stmt.setAllowBareNamedParameters(false)
setAllowUnknownNamedParameters(enabled) -> None
Sets whether named parameters the statement doesn't use are ignored, rather than throwing.
Declaration
setAllowUnknownNamedParameters(enabled) -> None
Parameters
| Name | Type | Description |
|---|---|---|
| enabled | JSValue | Whether to ignore unknown names |
Returns
None
Example
stmt.setAllowUnknownNamedParameters(true)