API

Database Objects

database.open(filename, function (error) {})

Open a database handle to the database at the specified filename. If the file
does not exist the bindings will attempt to create it. The callback takes no
arguments.

A filename of ":memory:" may be used to create an in-memory database.

database.close(function (error) {})

Close the database handle.

database.execute(sql[, bindings], function (error, rows) {})

Execute a SQL query, sql with optional bindings bindings on the currently
opened database. The callback will be executed once with all the rows returned
for the query. This is much faster than database.query since there are less roundtrips into the thread-pool.

database.query(sql, [bindings,] function (error, row) {})

Execute a SQL query, sql, with optional bindings bindings on the currently
opened database. The callback will be executed once per row returned, plus
once more with row set to undefined to indicate end of results.

Execute multiple semi-colon separated SQL statements. Statements must take no
placeholders. Each statement will be executed with a single step() and then
reset. This is ideally suited to executing multiple DDL statements.

database.prepare(SQL, [options,] function (error, statement) {})

Create a prepared statement from an SQL string. Prepared statements can be
used used to iterate over results and to avoid compiling SQL each time a query
is performed.

Options:

lastInsertRowID: boolean, default false.
If true, when this statement is step()'d over, the context object (this) in
the callback will contain a lastInsertRowID member with the ID of the last
inserted row.

affectedRows: boolean, default false.
If true, when this statement is step()'d over, the context object (this) in
the callback will contain an affectedRows member with the number of
affected rows for the last step.

statement.bind(position, value, function (error) {})

Bind a value to a place-holder position. Because binding place-holders is done
by position (not index), the first place-holder is at position 1, second at
place-holder position 2, etc.

statement.clearBindings()

Immediately clear the bindings from the statement. There is no callback.

statement.step(function (error, row) {})

Fetch one row from a prepared statement and hand it off to a callback. If
there are no more rows to be fetched, row will be undefined. Rows are
represented as objects with properties named after the respective columns.

statement.fetchAll(function (error, rows) {})

Fetch all rows in statement and pass them to the callback as an array of
objects, each object representing one row.

statement.reset()

Immediately reset a statement object back to it's initial state, ready to be
step() or fetchAll()'d again.

statement.finalize(function (error) {})

Free SQLite objects associated with this statement and mark it for garbage
collection.