Skip to main content

Connect to SQLite

Using the Deno SQLite3 module, you can connect to an SQLite3 database stored locally and perform basic database operations.

Edit on Github
Import the Database class from jsr:@db/sqlite
import { Database } from "jsr:@db/sqlite@0.12";
Open or create an SQLite database named 'test.db'
const db = new Database("test.db");
Create a table called "people" if it doesn't exist
db.prepare(
  `
	CREATE TABLE IF NOT EXISTS people (
	  id INTEGER PRIMARY KEY AUTOINCREMENT,
	  name TEXT,
	  age INTEGER
	);
  `,
).run();
Insert a new row into the "people" table
db.prepare(
  `
	INSERT INTO people (name, age) VALUES (?, ?);
  `,
).run("Bob", 40);
Query all rows from the "people" table
const rows = db.prepare("SELECT id, name, age FROM people").all();
console.log("People:");
for (const row of rows) {
  console.log(row);
}
Close the database connection
db.close();

Run this example locally using the Deno CLI:

deno run --allow-read --allow-write --allow-env --allow-net --allow-ffi https://docs.deno.com/examples/scripts/sqlite.ts

Additional resources

Did you find what you needed?

Privacy policy