Class Statement<ParamsType, ReturnType>

A prepared statement.

This is returned by prepare and query.

Example

const stmt = db.prepare("SELECT * FROM foo WHERE bar = ?");
stmt.all("baz");
// => [{bar: "baz"}]

Example

const stmt = db.prepare("SELECT * FROM foo WHERE bar = ?");
stmt.get("baz");
// => {bar: "baz"}

Example

const stmt = db.prepare("SELECT * FROM foo WHERE bar = ?");
stmt.run("baz");
// => undefined

Type Parameters

Hierarchy

  • Statement

Constructors

  • Creates a new prepared statement from native code.

    This is used internally by the Database class. Probably you don't need to call this yourself.

    Type Parameters

    Parameters

    • nativeHandle: any

    Returns Statement<ParamsType, ReturnType>

Properties

columnNames: string[]

The names of the columns returned by the prepared statement.

Example

const stmt = db.prepare("SELECT bar FROM foo WHERE bar = ?");

console.log(stmt.columnNames);
// => ["bar"]
native: any

Native object representing the underlying sqlite3_stmt

This is left untyped because the ABI of the native bindings may change at any time.

paramsCount: number

The number of parameters expected in the prepared statement.

Example

const stmt = db.prepare("SELECT * FROM foo WHERE bar = ?");
console.log(stmt.paramsCount);
// => 1

Example

const stmt = db.prepare("SELECT * FROM foo WHERE bar = ? AND baz = ?");
console.log(stmt.paramsCount);
// => 2

Methods

  • Execute the prepared statement and return all results as objects.

    Example

    const stmt = db.prepare("SELECT * FROM foo WHERE bar = ?");

    stmt.all("baz");
    // => [{bar: "baz"}]

    stmt.all();
    // => [{bar: "baz"}]

    stmt.all("foo");
    // => [{bar: "foo"}]

    Parameters

    • Rest ...params: ParamsType[]

      optional values to bind to the statement. If omitted, the statement is run with the last bound values or no parameters if there are none.

    Returns ReturnType[]

  • Finalize the prepared statement, freeing the resources used by the statement and preventing it from being executed again.

    This is called automatically when the prepared statement is garbage collected.

    It is safe to call this multiple times. Calling this on a finalized statement has no effect.

    Internally, this calls sqlite3_finalize.

    Returns void

  • Execute the prepared statement and return the first result.

    If no result is returned, this returns null.

    Example

    const stmt = db.prepare("SELECT * FROM foo WHERE bar = ?");

    stmt.all("baz");
    // => [{bar: "baz"}]

    stmt.all();
    // => [{bar: "baz"}]

    stmt.all("foo");
    // => [{bar: "foo"}]

    The following types can be used when binding parameters:

    JavaScript type SQLite type
    string TEXT
    number INTEGER or DECIMAL
    boolean INTEGER (1 or 0)
    Uint8Array BLOB
    Buffer BLOB
    bigint INTEGER
    null NULL

    Parameters

    • Rest ...params: ParamsType[]

      optional values to bind to the statement. If omitted, the statement is run with the last bound values or no parameters if there are none.

    Returns ReturnType

  • Execute the prepared statement. This returns undefined.

    Example

    const stmt = db.prepare("UPDATE foo SET bar = ?");
    stmt.run("baz");
    // => undefined

    stmt.run();
    // => undefined

    stmt.run("foo");
    // => undefined

    The following types can be used when binding parameters:

    JavaScript type SQLite type
    string TEXT
    number INTEGER or DECIMAL
    boolean INTEGER (1 or 0)
    Uint8Array BLOB
    Buffer BLOB
    bigint INTEGER
    null NULL

    Parameters

    • Rest ...params: ParamsType[]

      optional values to bind to the statement. If omitted, the statement is run with the last bound values or no parameters if there are none.

    Returns void

  • Return the expanded SQL string for the prepared statement.

    Internally, this calls sqlite3_expanded_sql() on the underlying sqlite3_stmt.

    Example

    const stmt = db.prepare("SELECT * FROM foo WHERE bar = ?", "baz");
    console.log(stmt.toString());
    // => "SELECT * FROM foo WHERE bar = 'baz'"
    console.log(stmt);
    // => "SELECT * FROM foo WHERE bar = 'baz'"

    Returns string

  • Execute the prepared statement and return the results as an array of arrays.

    This is a little faster than all.

    Example

    const stmt = db.prepare("SELECT * FROM foo WHERE bar = ?");

    stmt.values("baz");
    // => [['baz']]

    stmt.values();
    // => [['baz']]

    stmt.values("foo");
    // => [['foo']]

    The following types can be used when binding parameters:

    JavaScript type SQLite type
    string TEXT
    number INTEGER or DECIMAL
    boolean INTEGER (1 or 0)
    Uint8Array BLOB
    Buffer BLOB
    bigint INTEGER
    null NULL

    Parameters

    • Rest ...params: ParamsType[]

      optional values to bind to the statement. If omitted, the statement is run with the last bound values or no parameters if there are none.

    Returns (string | number | bigint | boolean | Uint8Array)[][]

Generated using TypeDoc