Node.js v24.6.0 documentation
- Node.js v24.6.0
-
Table of contents
- SQLite
- Class:
DatabaseSyncnew DatabaseSync(path[, options])database.aggregate(name, options)database.close()database.loadExtension(path)database.enableLoadExtension(allow)database.location([dbName])database.exec(sql)database.function(name[, options], function)database.isOpendatabase.isTransactiondatabase.open()database.prepare(sql)database.createSession([options])database.applyChangeset(changeset[, options])database[Symbol.dispose]()
- Class:
Session - Class:
StatementSyncstatement.all([namedParameters][, ...anonymousParameters])statement.columns()statement.expandedSQLstatement.get([namedParameters][, ...anonymousParameters])statement.iterate([namedParameters][, ...anonymousParameters])statement.run([namedParameters][, ...anonymousParameters])statement.setAllowBareNamedParameters(enabled)statement.setAllowUnknownNamedParameters(enabled)statement.setReturnArrays(enabled)statement.setReadBigInts(enabled)statement.sourceSQL- Type conversion between JavaScript and SQLite
sqlite.backup(sourceDb, path[, options])sqlite.constants
- Class:
- SQLite
-
Index
- Assertion testing
- Asynchronous context tracking
- Async hooks
- Buffer
- C++ addons
- C/C++ addons with Node-API
- C++ embedder API
- Child processes
- Cluster
- Command-line options
- Console
- Crypto
- Debugger
- Deprecated APIs
- Diagnostics Channel
- DNS
- Domain
- Environment Variables
- Errors
- Events
- File system
- Globals
- HTTP
- HTTP/2
- HTTPS
- Inspector
- Internationalization
- Modules: CommonJS modules
- Modules: ECMAScript modules
- Modules:
node:moduleAPI - Modules: Packages
- Modules: TypeScript
- Net
- OS
- Path
- Performance hooks
- Permissions
- Process
- Punycode
- Query strings
- Readline
- REPL
- Report
- Single executable applications
- SQLite
- Stream
- String decoder
- Test runner
- Timers
- TLS/SSL
- Trace events
- TTY
- UDP/datagram
- URL
- Utilities
- V8
- VM
- WASI
- Web Crypto API
- Web Streams API
- Worker threads
- Zlib
- Other versions
- Options
SQLite#
Stability: 1.1 - Active development.
Source Code: lib/sqlite.js
The node:sqlite module facilitates working with SQLite databases.
To access it:
import sqlite from 'node:sqlite';const sqlite = require('node:sqlite');
This module is only available under the node: scheme.
The following example shows the basic usage of the node:sqlite module to open
an in-memory database, write data to the database, and then read the data back.
import { DatabaseSync } from 'node:sqlite';
const database = new DatabaseSync(':memory:');
// Execute SQL statements from strings.
database.exec(`
CREATE TABLE data(
key INTEGER PRIMARY KEY,
value TEXT
) STRICT
`);
// Create a prepared statement to insert data into the database.
const insert = database.prepare('INSERT INTO data (key, value) VALUES (?, ?)');
// Execute the prepared statement with bound values.
insert.run(1, 'hello');
insert.run(2, 'world');
// Create a prepared statement to read data from the database.
const query = database.prepare('SELECT * FROM data ORDER BY key');
// Execute the prepared statement and log the result set.
console.log(query.all());
// Prints: [ { key: 1, value: 'hello' }, { key: 2, value: 'world' } ]'use strict';
const { DatabaseSync } = require('node:sqlite');
const database = new DatabaseSync(':memory:');
// Execute SQL statements from strings.
database.exec(`
CREATE TABLE data(
key INTEGER PRIMARY KEY,
value TEXT
) STRICT
`);
// Create a prepared statement to insert data into the database.
const insert = database.prepare('INSERT INTO data (key, value) VALUES (?, ?)');
// Execute the prepared statement with bound values.
insert.run(1, 'hello');
insert.run(2, 'world');
// Create a prepared statement to read data from the database.
const query = database.prepare('SELECT * FROM data ORDER BY key');
// Execute the prepared statement and log the result set.
console.log(query.all());
// Prints: [ { key: 1, value: 'hello' }, { key: 2, value: 'world' } ]
Class: DatabaseSync#
This class represents a single connection to a SQLite database. All APIs exposed by this class execute synchronously.
new DatabaseSync(path[, options])#
path<string> | <Buffer> | <URL> The path of the database. A SQLite database can be stored in a file or completely in memory. To use a file-backed database, the path should be a file path. To use an in-memory database, the path should be the special name':memory:'.options<Object> Configuration options for the database connection. The following options are supported:open<boolean> Iftrue, the database is opened by the constructor. When this value isfalse, the database must be opened via theopen()method. Default:true.readOnly<boolean> Iftrue, the database is opened in read-only mode. If the database does not exist, opening it will fail. Default:false.enableForeignKeyConstraints<boolean> Iftrue, foreign key constraints are enabled. This is recommended but can be disabled for compatibility with legacy database schemas. The enforcement of foreign key constraints can be enabled and disabled after opening the database usingPRAGMA foreign_keys. Default:true.enableDoubleQuotedStringLiterals<boolean> Iftrue, SQLite will accept double-quoted string literals. This is not recommended but can be enabled for compatibility with legacy database schemas. Default:false.allowExtension<boolean> Iftrue, theloadExtensionSQL function and theloadExtension()method are enabled. You can callenableLoadExtension(false)later to disable this feature. Default:false.timeout<number> The busy timeout in milliseconds. This is the maximum amount of time that SQLite will wait for a database lock to be released before returning an error. Default:0.readBigInts<boolean> Iftrue, integer fields are read as JavaScriptBigIntvalues. Iffalse, integer fields are read as JavaScript numbers. Default:false.returnArrays<boolean> Iftrue, query results are returned as arrays instead of objects. Default:false.allowBareNamedParameters<boolean> Iftrue, allows binding named parameters without the prefix character (e.g.,fooinstead of:foo). Default:true.allowUnknownNamedParameters<boolean> Iftrue, unknown named parameters are ignored when binding. Iffalse, an exception is thrown for unknown named parameters. Default:false.
Constructs a new DatabaseSync instance.
database.aggregate(name, options)#
Registers a new aggregate function with the SQLite database. This method is a wrapper around
sqlite3_create_window_function().
name<string> The name of the SQLite function to create.options<Object> Function configuration settings.deterministic<boolean> Iftrue, theSQLITE_DETERMINISTICflag is set on the created function. Default:false.directOnly<boolean> If