SQLite

database.prepare()

Compiles SQL for later execution with bound values. Parameters replace data, not table names or SQL keywords. The synchronous API can block the event loop; the 22.16.0 module is experimental. This fixture uses an isolated in-memory database. Tested on Node.js 22.16.0. Example output (one line per entry): ["O'Reilly"].

Formal Syntax & Structure

nodejs syntaxnodejs
database.prepare(sql)

Parameters & Attributes

NameTypeDescription
sqlstringSQL statement.

Return Value

StatementSync

Prepared statement.

Working Example

Example Snippetnodejs
import { DatabaseSync } from 'node:sqlite';const db = new DatabaseSync(':memory:');try {    db.exec('CREATE TABLE book(title TEXT)');    const insert = db.prepare('INSERT INTO book VALUES (?)');    insert.run("O'Reilly");    console.log(db.prepare('SELECT title FROM book').get().title);}finally {    db.close();}
Node.js Reference