Introduction
SQLite lets you store data right inside your app without needing a separate database server. It is simple and fast for small to medium projects.
SQLite lets you store data right inside your app without needing a separate database server. It is simple and fast for small to medium projects.
import { DB } from "https://deno.land/x/sqlite/mod.ts"; const db = new DB("mydata.db"); db.query("CREATE TABLE IF NOT EXISTS users (id INTEGER PRIMARY KEY AUTOINCREMENT, name TEXT)"); db.query("INSERT INTO users (name) VALUES (?)", ["Alice"]); for (const [id, name] of db.query("SELECT id, name FROM users")) { console.log(id, name); } db.close();
DB from the SQLite module to create or open a database file.db.close() when done to save changes.const db = new DB(); // Creates an in-memory database
// Data is lost when program endsconst db = new DB("appdata.db"); // Opens or creates a file database // Data is saved in 'appdata.db' file
db.query("CREATE TABLE IF NOT EXISTS tasks (id INTEGER PRIMARY KEY AUTOINCREMENT, title TEXT, done BOOLEAN)");db.query("INSERT INTO tasks (title, done) VALUES (?, ?)", ["Learn Deno", false]);
This program creates a tasks database, adds three tasks, then prints each task with its done status.
import { DB } from "https://deno.land/x/sqlite/mod.ts"; const db = new DB("tasks.db"); // Create table if not exists db.query(`CREATE TABLE IF NOT EXISTS tasks ( id INTEGER PRIMARY KEY AUTOINCREMENT, title TEXT, done BOOLEAN )`); // Insert some tasks const tasks = [ ["Buy groceries", false], ["Read a book", true], ["Write code", false] ]; for (const [title, done] of tasks) { db.query("INSERT INTO tasks (title, done) VALUES (?, ?)", [title, done]); } // Query and print tasks for (const [id, title, done] of db.query("SELECT id, title, done FROM tasks")) { console.log(`Task ${id}: ${title} - Done: ${done}`); } db.close();
SQLite stores Boolean values as 0 (false) or 1 (true).
Use parameter placeholders ? to safely insert data and avoid errors.
Remember to close the database to save changes and free resources.
SQLite is a simple, file-based database good for small apps and local data storage.
Use Deno's SQLite module to create, query, and manage databases easily.
Always close your database connection when finished.
import { DB } from "https://deno.land/x/sqlite/mod.ts";
const db = new DB();
db.query("CREATE TABLE users (id INTEGER PRIMARY KEY, name TEXT)");
db.query("INSERT INTO users (name) VALUES (?)", ["Alice"]);
const result = [...db.query("SELECT name FROM users")];
console.log(result);
db.close();import { DB } from "https://deno.land/x/sqlite/mod.ts";
const db = new DB("mydb.sqlite");
db.query("CREATE TABLE items (id INTEGER PRIMARY KEY, name TEXT)");
db.query("INSERT INTO items (name) VALUES (?)", ["Book"]);
// Missing db.close()import { DB } from "https://deno.land/x/sqlite/mod.ts";
const db = new DB();
// Which code below is correct?Pass the test and this pattern moves unknown