method

sqlite.Database.run

run<ParamsType extends SQLQueryBindings[]>(
sql: string,
...bindings: ParamsType[]

Execute a SQL query without returning any results.

This does not cache the query. To run a query multiple times, use prepare instead.

Internally, this calls sqlite3_prepare_v3 followed by sqlite3_step and sqlite3_finalize.

The string can hold several statements, separated by ;. They run in order, and the first one gets the bindings. The first statement that fails throws, and the statements after it do not run.

The following types can be used when binding parameters:

JavaScript typeSQLite type
stringTEXT
numberINTEGER or REAL
booleanINTEGER (1 or 0)
Uint8ArrayBLOB
BufferBLOB
bigintINTEGER
nullNULL

Useful for queries like:

  • CREATE TABLE
  • INSERT INTO
  • UPDATE
  • DELETE FROM
  • DROP TABLE
  • PRAGMA
  • ATTACH DATABASE
  • DETACH DATABASE
  • REINDEX
  • VACUUM
  • EXPLAIN ANALYZE
  • CREATE INDEX
  • CREATE TRIGGER
  • CREATE VIEW
  • CREATE VIRTUAL TABLE
  • CREATE TEMPORARY TABLE
@param sql

The SQL query to run

@param bindings

Optional bindings for the query

@returns

A Changes object with changes and lastInsertRowid properties

db.run("CREATE TABLE foo (bar TEXT)");
db.run("INSERT INTO foo VALUES (?)", ["baz"]);
// => { changes: 1, lastInsertRowid: 1 }

Referenced types

interface Changes

An object representing the changes made to the database since the last run or exec call.

  • changes: number

    The number of rows that the last run or exec call inserted, updated, or deleted itself.

    Rows that a trigger or a foreign key action such as ON DELETE CASCADE changes do not count. Database.run with several statements in one string reports their sum.

  • lastInsertRowid: number | bigint

    The rowid of the most recently inserted row.

    If safeIntegers is true, this is a bigint. Otherwise, it is a number.