import { Database } from "https://deno.land/x/sqlite3@0.4.4/mod.ts";
Represents a SQLite3 database connection.
Example:
// Open a database from file, creates if doesn't exist.
const db = new Database("myfile.db");
// Open an in-memory database.
const db = new Database(":memory:");
// Open a read-only database.
const db = new Database("myfile.db", { readonly: true });
// Or open using File URL
const db = new Database(new URL("./myfile.db", import.meta.url));
Constructors
Properties
Methods
Simply executes the SQL, without returning anything.
Example:
// Create table
db.execute("create table users (id integer not null, username varchar(20) not null)");
// Inserts
db.execute("insert into users (id, username) values(?, ?)", id, username);
// Or run SQL safely using Template Strings!
db.execute`insert into users (id, username) values(${id}, ${username})`;
// Insert with named parameters
db.execute("insert into users (id, username) values(:id, :username)", { id, username });
// Pragma statements
db.execute("pragma journal_mode = WAL");
db.execute("pragma synchronous = normal");
db.execute("pragma temp_store = memory");
Under the hood, it uses sqlite3_exec
if no parameters are given to bind
with the SQL statement, a prepared statement otherwise.
Open a Blob for incremental I/O.
Make sure to close the blob after you are done with it, otherwise you will have memory leaks.
Creates a new prepared statement.
Example:
const stmt = db.prepare("insert into users (id, username) values (?, ?)");
for (const user of usersToInsert) {
stmt.execute(id, user);
}
stmt.finalize();
Runs an SQL query with given parameters, and returns rows as array of columns.
If you need the rows as objects, use queryObject
instead. However, it is
recommended to use queryArray
because of the extra overhead added by FFI
calls to get column names to create row objects.
Example:
const users = db.queryArray<[number, string]>("select id, username from users");
// Using bind parameters
const [user] = db.queryArray<[number, string]>("select id, username from users where email = ?", email);
// Using template strings
const [user] = db.queryArray<[number, string]>`select id, username from users where email = ${email}`;
// Using named bind parameters
const [user] = db.queryArray<[number, string]>("select id, username from users where email = :email", { email });
Executes an SQL query and returns the rows as objects.
Note: if you do not need the column names, consider calling queryArray
instead.
As this method does an extra FFI call to get the column names, it is more expensive than queryArray
.
Example:
const users = db.queryObject<{
id: number,
username: string,
}>("select id, username from users");
// Using bind parameters
const [user] = db.queryObject<{
id: number,
username: string,
}>("select id, username from users where email = ?", email);
// Using template strings
const [user] = db.queryObject<{
id: number,
username: string,
}>`select id, username from users where email = ${email}`;
// Using named bind parameters
const [user] = db.queryObject<{
id: number,
username: string,
}>("select id, username from users where email = :email", { email });