Database object is returned by connect(). It holds the connection state and is used to prepare statements and run ad-hoc SQL.
connect(path, options?)
Opens a connection to a database file and returns a Database instance.
Path to the database file. Use
":memory:" for a transient in-memory database. The file is created when it does not exist.Optional configuration object.
Database
db.prepare(sql)
Compiles a SQL statement and returns a reusable Statement object.
The SQL statement to compile.
Statement — see Statement for the full method list.
db.exec(sql)
Executes one or more SQL statements without returning rows. Useful for DDL and scripts.
One or more SQL statements separated by semicolons.
this
db.close()
Closes the database connection and finalizes all outstanding prepared statements. Any further use of the Database or any of its Statement objects will throw.
this
db.lastInsertRowid()
Returns the rowid of the row most recently inserted by this connection.
Returns number
db.changes()
Returns the number of rows modified by the most recent INSERT, UPDATE, or DELETE statement.
Returns number
db.totalChanges()
Returns the total number of rows modified since the connection was opened.
Returns number
db.transaction(fn)
Wraps a function in a transaction. Returns a new function that, when called, executes the original function within a SQLite transaction.
The function to wrap. It receives the same arguments as the returned function.
.deferred, .immediate, and .exclusive properties for controlling the transaction mode.
db.defaultSafeIntegers(toggle?)
Sets the default safeIntegers mode for all new statements prepared on this connection. When enabled, 64-bit integers are returned as BigInt instead of number.
Pass
false to disable. Omit or pass true to enable.Properties
| Property | Type | Description |
|---|---|---|
readonly | boolean | Whether the connection is in read-only mode. |
memory | boolean | Whether this is an in-memory database. |
path | string | File path the connection was opened with. |
open | boolean | Whether the connection is still open. |
Unsupported methods
The following methods from thebetter-sqlite3 API are not yet implemented:
These methods throw or are no-ops when called. They are listed here for
reference when migrating from
better-sqlite3.| Method | Status |
|---|---|
db.pragma(string, options?) | Not supported |
db.backup(destination, options?) | Not supported |
db.serialize(options?) | Not supported |
db.function(name, options?, fn) | Not supported |
db.aggregate(name, options) | Not supported |
db.table(name, definition) | Not supported |
db.authorizer(rules) | Not supported |
db.loadExtension(path, entryPoint?) | Not supported |
db.interrupt() | Not supported |