Bind value to prepared statement
This convenience function calls the appropriate bind_*
function
based on the type of value
. Note that binding indices begin with 1.
prepared statement pointer
binding index
SQLITE_OK
(throws exception on error)
Bind blob to prepared statement parameter
Note that binding indices begin with 1.
prepared statement pointer
binding index
SQLITE_OK
(throws exception on error)
Bind a collection of values to a statement
This convenience function binds values from either an array or object to a prepared statement with placeholder parameters.
Array example using numbered parameters (numbering is implicit in this example):
const str = sqlite3.str_new(db, `
INSERT INTO tbl VALUES (?, ?, ?);
`);
const prepared = await sqlite3.prepare_v2(db, sqlite3.str_value(str));
sqlite3.bind_collection(prepared.stmt, [42, 'hello', null]);
...
Object example using named parameters (':', '@', or '$' prefixes are allowed):
const str = sqlite3.str_new(db, `
INSERT INTO tbl VALUES (@foo, @bar, @baz);
`);
const prepared = await sqlite3.prepare_v2(db, sqlite3.str_value(str));
sqlite3.bind_collection(prepared.stmt, {
'@foo': 42,
'@bar': 'hello',
'@baz': null,
});
...
Note that SQLite bindings are indexed beginning with 1, but when
binding values from an array a
the values begin with a[0]
.
prepared statement pointer
SQLITE_OK
(throws exception on error)
Bind number to prepared statement parameter
Note that binding indices begin with 1.
prepared statement pointer
binding index
SQLITE_OK
(throws exception on error)
Bind number to prepared statement parameter
Note that binding indices begin with 1.
prepared statement pointer
binding index
SQLITE_OK
(throws exception on error)
Bind number to prepared statement parameter
Note that binding indices begin with 1.
prepared statement pointer
binding index
SQLITE_OK
(throws exception on error)
Bind null to prepared statement
Note that binding indices begin with 1.
prepared statement pointer
SQLITE_OK
(throws exception on error)
Get number of bound parameters
prepared statement pointer
number of statement binding locations
Get name of bound parameter
Note that binding indices begin with 1.
prepared statement pointer
binding index
binding name
Bind string to prepared statement
Note that binding indices begin with 1.
prepared statement pointer
binding index
SQLITE_OK
(throws exception on error)
Get count of rows modified by last insert/update
database pointer
number of rows modified
Close database connection
database pointer
SQLITE_OK
(throws exception on error)
Call the appropriate column_*
function based on the column type
The type is determined by calling column_type, which may
not match the type declared in CREATE TABLE
. Note that if the column
value is a blob then as with column_blob
the result may be invalid
after the next SQLite call; copy if it needs to be retained.
Integer values are returned as Number if within the min/max safe integer bounds, otherwise they are returned as BigInt.
prepared statement pointer
column index
column value
Extract a column value from a row after a prepared statment step
The contents of the returned buffer may be invalid after the
next SQLite call. Make a copy of the data (e.g. with .slice()
)
if longer retention is required.
prepared statement pointer
column index
column value
Get storage size for column text or blob
prepared statement pointer
column index
number of bytes in column text or blob
Get number of columns for a prepared statement
prepared statement pointer
number of columns
Extract a column value from a row after a prepared statment step
prepared statement pointer
column index
column value
Extract a column value from a row after a prepared statment step
prepared statement pointer
column index
column value
Extract a column value from a row after a prepared statment step
prepared statement pointer
column index
column value
Get a column name for a prepared statement
prepared statement pointer
column index
column name
Get names for all columns of a prepared statement
This is a convenience function that calls column_count and column_name.
array of column names
Extract a column value from a row after a prepared statment step
prepared statement pointer
column index
column value
Get column type for a prepared statement
Note that this type may not match the type declared in CREATE TABLE
.
prepared statement pointer
column index
enumeration value for type
Create or redefine SQL functions
database pointer
number of function arguments
text encoding (and other flags)
application data
SQLITE_OK
(throws exception on error)
Create a SQLite module for virtual tables
database pointer
SQLITE_OK
(throws exception on error)
Get number of columns in current row of a prepared statement
prepared statement pointer
number of columns
Declare the schema of a virtual table in module SQLiteModule.xCreate or SQLiteModule.xConnect methods
database pointer
schema declaration
SQLITE_OK
(throws exception on error)
One-step query execution interface
The implementation of this function uses row, which makes a copy of blobs and returns BigInt for integers outside the safe integer bounds for Number.
database pointer
queries
called for each output row
Promise resolving to SQLITE_OK
(rejects on error)
Destroy a prepared statement object compiled with prepare_v2
This function does not throw on error.
prepared statement pointer
Promise resolving to SQLITE_OK
or error status
Test for autocommit mode
database pointer
Non-zero if autocommit mode is on, zero otherwise
Get SQLite library version
version string, e.g. '3.35.5'
Get SQLite library version
version number, e.g. 3035005
Set a usage limit on a connection.
database pointer
limit category
previous setting
Opening a new database connection.
Note that this function differs from the C API in that it returns the Promise-wrapped database pointer (instead of a result code).
SQLite.SQLITE_OPEN_CREATE | SQLite.SQLITE_OPEN_READWRITE
(0x6) if omitted
VFS name
Promise-wrapped database pointer.
Compile an SQL statement
SQL is provided as a pointer in WASM memory, so the utility functions
str_new and str_value should be used. The returned
Promise-wrapped object provides both the prepared statement and a
pointer to the still uncompiled SQL that can be used with the next
call to this function. A Promise containing null
is returned
when no statement remains.
Each prepared statement should be destroyed with finalize after its usage is complete.
Code using prepare_v2 generally looks like this:
const str = sqlite3.str_new(db, sql);
try {
// Traverse and prepare the SQL, statement by statement.
let prepared = { stmt: null, sql: sqlite3.str_value(str) };
while ((prepared = await sqlite3.prepare_v2(db, prepared.sql))) {
try {
// Step through the rows produced by the statement.
while (await sqlite3.step(prepared.stmt) === SQLite.SQLITE_ROW) {
// Do something with the row data...
}
} finally {
sqlite3.finalize(prepared.stmt);
}
}
} finally {
sqlite3.str_finish(str);
}
The statements convenience function can be used to avoid the boilerplate of calling prepare_v2 directly.
database pointer
SQL pointer
Promise-wrapped object containing the prepared statement
pointer and next SQL pointer, or a Promise containing null
when
no statement remains
Specify callback to be invoked between long-running queries
database pointer
target number of database operations between handler invocations
Reset a prepared statement object
prepared statement pointer
Promise-wrapped SQLITE_OK
(rejects on error)
Convenience function to call result_*
based of the type of value
context pointer
Set the result of a function or vtable column
context pointer
Set the result of a function or vtable column
context pointer
Set the result of a function or vtable column
context pointer
Set the result of a function or vtable column
context pointer
Set the result of a function or vtable column
context pointer
Set the result of a function or vtable column
context pointer
Get all column data for a row from a prepared statement step
This convenience function will return a copy of any blob, unlike column_blob which returns a value referencing volatile WASM memory with short validity. Like column, it will return a BigInt for integers outside the safe integer bounds for Number.
prepared statement pointer
row data
Register a callback function that is invoked to authorize certain SQL statement actions.
database pointer
Get statement SQL
prepared statement pointer
SQL
SQL statement iterator
This is a convenience function that manages statement compilation,
replacing boilerplate code associated with calling prepare_v2
directly. It is typically used with a for await
loop (in an
async function), like this:
// Compile one statement on each iteration of this loop.
for await (const stmt of sqlite3.statements(db, sql)) {
// Bind parameters here if using SQLite placeholders.
// Execute the statement with this loop.
while (await sqlite3.step(stmt) === SQLite.SQLITE_ROW) {
// Collect row data here.
}
// Change bindings, reset, and execute again if desired.
}
finalize should not be called on a statement provided by the iterator; the statement resources will be released automatically at the end of each iteration. This also means that the statement is only valid within the scope of the loop - use prepare_v2 directly to compile a statement with an application-specified lifetime.
If using the iterator manually, i.e. by calling its next
method, be sure to call the return
method if iteration
is abandoned before completion (for await
and other implicit
traversals provided by Javascript do this automatically)
to ensure that all allocated resources are released.
database pointer
Evaluate an SQL statement
prepared statement pointer
Promise resolving to SQLITE_ROW
or SQLITE_DONE
(rejects on error)
Add content to a sqlite3_str
dynamic string
Not recommended for building strings incrementally; prefer using Javascript and str_new with initialization.
sqlite3_str
pointer
string to append
Finalize a sqlite3_str
dynamic string created with str_new
sqlite3_str
pointer
Create a new sqlite3_str
dynamic string instance
The purpose for sqlite3_str
is to transfer a SQL string in
Javascript to WebAssembly memory for use with prepare_v2.
An optional initialization argument has been added for convenience which is functionally equivalent to (but slightly more efficient):
const str = sqlite3.str_new(db);
sqlite3.str_appendall(str, s);
A sqlite3_str
instance should always be destroyed with
str_finish after use to avoid a resource leak.
database pointer
optional initialization string
sqlite3_str
pointer
Get pointer to sqlite3_str
dynamic string data
The returned pointer points to the UTF-8 encoded string in WebAssembly memory. Use as input with prepare_v2.
sqlite3_str
pointer
pointer to string data
Get application data in custom function implementation
context pointer
application data
Extract a value from sqlite3_value
This is a convenience function that calls the appropriate value_*
function based on its type. Note that if the value is a blob then as
with value_blob
the result may be invalid after the next SQLite call.
Integer values are returned as Number if within the min/max safe integer bounds, otherwise they are returned as BigInt.
sqlite3_value
pointer
value
Extract a value from sqlite3_value
The contents of the returned buffer may be invalid after the
next SQLite call. Make a copy of the data (e.g. with .slice()
)
if longer retention is required.
sqlite3_value
pointer
value
Get blob or text size for value
sqlite3_value
pointer
size
Extract a value from sqlite3_value
sqlite3_value
pointer
value
Extract a value from sqlite3_value
sqlite3_value
pointer
value
Extract a value from sqlite3_value
sqlite3_value
pointer
value
Extract a value from sqlite3_value
sqlite3_value
pointer
value
Get type of sqlite3_value
sqlite3_value
pointer
enumeration value for type
Register a new Virtual File System.
VFS object
SQLITE_OK
(throws exception on error)
Generated using TypeDoc
Javascript wrappers for the SQLite C API (plus a few convenience functions)
Function signatures have been slightly modified to be more Javascript-friendly. For the C functions that return an error code, the corresponding Javascript wrapper will throw an exception with a
code
property on an error.Note that a few functions return a Promise in order to accomodate either a synchronous or asynchronous SQLite build, generally those involved with opening/closing a database or executing a statement.
To create an instance of the API, follow these steps:
https://sqlite.org/c3ref/funclist.html