Connect to DuckDB
Using Deno with DuckDB, you can connect to memory or a persistent database with a filename.
import { open } from "https://deno.land/x/duckdb/mod.ts";
const db = open("./example.db");
const db = open(":memory:");
const connection = db.connect();
for (const row of connection.stream("select 42 as number")) {
console.debug(`Row Number: ${row.number}`); // -> { number: 42 }
}
const prepared = connection.prepare(
"SELECT ?::INTEGER AS number, ?::VARCHAR AS text;",
);
const result = prepared.query(1337, "foo"); // [{ number: 1337, text: 'foo' }]
console.debug(`Number: ${result[0].number}`);
console.debug(`Text: ${result[0].text}`);
connection.close();
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/duckdb.ts