Consider this Deno code using SQLite:
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 id, name FROM users")];
db.close();
console.log(result);What will be logged to the console?
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 id, name FROM users")]; db.close(); console.log(result);
Remember that db.query returns rows as arrays, not objects.
The db.query method returns an iterable of rows as arrays. Since we inserted one user with id 1 and name "Alice", the result is an array containing one array: [[1, "Alice"]].
In Deno, you want to create an in-memory SQLite database. Which code snippet does this correctly?
SQLite uses a special filename to indicate in-memory mode.
Passing the string ":memory:" to the DB constructor opens an in-memory database. The default constructor new DB() creates a temporary file-based database, not in-memory.
Given this code snippet:
import { DB } from "https://deno.land/x/sqlite/mod.ts";
const db = new DB();
db.query("CREATE TABLE items (id INTEGER PRIMARY KEY, name TEXT)");
db.query("INSERT INTO items (id, name) VALUES (?, ?)", [1]);
db.close();What error will this code produce and why?
import { DB } from "https://deno.land/x/sqlite/mod.ts"; const db = new DB(); db.query("CREATE TABLE items (id INTEGER PRIMARY KEY, name TEXT)"); db.query("INSERT INTO items (id, name) VALUES (?, ?)", [1]); db.close();
Check the number of placeholders and parameters passed.
The SQL statement expects two parameters for the placeholders (?, ?), but only one parameter is provided in the array. This mismatch causes an error about parameter count.
Analyze this code:
import { DB } from "https://deno.land/x/sqlite/mod.ts";
const db = new DB();
db.query("CREATE TABLE logs (id INTEGER PRIMARY KEY, message TEXT)");
db.query("INSERT INTO logs (message) VALUES (?)", ["start"]);
db.query("INSERT INTO logs (message) VALUES (?)", ["process"]);
const [[count]] = [...db.query("SELECT COUNT(*) FROM logs")];
db.close();
console.log(count);What will be printed?
import { DB } from "https://deno.land/x/sqlite/mod.ts"; const db = new DB(); db.query("CREATE TABLE logs (id INTEGER PRIMARY KEY, message TEXT)"); db.query("INSERT INTO logs (message) VALUES (?)", ["start"]); db.query("INSERT INTO logs (message) VALUES (?)", ["process"]); const [[count]] = [...db.query("SELECT COUNT(*) FROM logs")]; db.close(); console.log(count);
Count how many rows were inserted before the SELECT COUNT(*) query.
Two rows were inserted into the logs table, so the COUNT(*) query returns 2.
Choose the correct statement about using SQLite embedded in Deno:
Think about how SQLite works as an embedded database.
SQLite is an embedded database and in Deno it is accessed synchronously. It does not require a server process, does not support multi-threaded concurrent writes by default, and can be used in-memory by specifying ":memory:".
