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
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.
run(...params) -> { changes: number | bigint, lastInsertRowid: number | bigint }
| Name | Type | Description |
|---|---|---|
| ...params | any[] | Named parameters (an object) and/or anonymous parameters |
BigInts 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 |
returnArrays is set), or undefined if there are no rowsconst 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 |
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 |
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 }>
name), and, for a column read directly from a table, the original column name, database, table and declared typeconst names = db.prepare('SELECT * FROM notes').columns().map(c => c.name)
setReadBigInts(enabled) -> None
Sets whether INTEGER values are read as BigInts, including run()'s changes and
lastInsertRowid.
setReadBigInts(enabled) -> None
| Name | Type | Description |
|---|---|---|
| enabled | JSValue | Whether to read BigInts |
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 |
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 |
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 |
stmt.setAllowUnknownNamedParameters(true)