Usage in Deno
import * as mod from "node:sqlite";
This module has been added in Deno v2.2.
The node:sqlite
module facilitates working with SQLite databases.
To access it:
import sqlite from 'node:sqlite';
This module is only available under the node:
scheme. The following will not
work:
import sqlite from 'sqlite';
The following example shows the basic usage of the node:sqlite
module to open
an in-memory database, write data to the database, and then read the data back.
import { DatabaseSync } from 'node:sqlite';
const database = new DatabaseSync(':memory:');
// Execute SQL statements from strings.
database.exec(`
CREATE TABLE data(
key INTEGER PRIMARY KEY,
value TEXT
) STRICT
`);
// Create a prepared statement to insert data into the database.
const insert = database.prepare('INSERT INTO data (key, value) VALUES (?, ?)');
// Execute the prepared statement with bound values.
insert.run(1, 'hello');
insert.run(2, 'world');
// Create a prepared statement to read data from the database.
const query = database.prepare('SELECT * FROM data ORDER BY key');
// Execute the prepared statement and log the result set.
console.log(query.all());
// Prints: [ { key: 1, value: 'hello' }, { key: 2, value: 'world' } ]
Classes #
This class represents a single connection to a SQLite database. All APIs exposed by this class execute synchronously.
This class represents a single prepared statement. This class cannot be
instantiated via its constructor. Instead, instances are created via thedatabase.prepare()
method. All APIs exposed by this class execute
synchronously.
Interfaces #
Namespaces #
Type Aliases #
Variables #
This constant is passed to the conflict handler while processing an INSERT change if the operation would result in duplicate primary key values.
The conflict handler is invoked with this constant when processing a DELETE or UPDATE change if a row with the required PRIMARY KEY fields is present in the database, but one or more other (non primary-key) fields modified by the update do not contain the expected "before" values.
If foreign key handling is enabled, and applying a changeset leaves the database in a state containing foreign key violations, the conflict handler is invoked with this constant exactly once before the changeset is committed. If the conflict handler returns SQLITE_CHANGESET_OMIT
, the changes, including those that caused the foreign key constraint violation, are committed. Or, if it returns SQLITE_CHANGESET_ABORT
, the changeset is rolled back.
The conflict handler is invoked with this constant when processing a DELETE or UPDATE change if a row with the required PRIMARY KEY fields is not present in the database.
Conflicting changes replace existing values. Note that this value can only be returned when the type of conflict is either SQLITE_CHANGESET_DATA
or SQLITE_CHANGESET_CONFLICT
.