This class represents a single connection to a SQLite database. All APIs exposed by this class execute synchronously.
class
sqlite.DatabaseSync
class DatabaseSync
Closes the database connection. If the database connection is already closed then this is a no-op.
- ): boolean;
An exception is thrown if the database is not open. This method is a wrapper around
sqlite3changeset_apply()
.const sourceDb = new DatabaseSync(':memory:'); const targetDb = new DatabaseSync(':memory:'); sourceDb.exec('CREATE TABLE data(key INTEGER PRIMARY KEY, value TEXT)'); targetDb.exec('CREATE TABLE data(key INTEGER PRIMARY KEY, value TEXT)'); const session = sourceDb.createSession(); const insert = sourceDb.prepare('INSERT INTO data (key, value) VALUES (?, ?)'); insert.run(1, 'hello'); insert.run(2, 'world'); const changeset = session.changeset(); targetDb.applyChangeset(changeset); // Now that the changeset has been applied, targetDb contains the same data as sourceDb.
@param changesetA binary changeset or patchset.
@param optionsThe configuration options for how the changes will be applied.
@returnsWhether the changeset was applied successfully without being aborted.
Closes the database connection. An exception is thrown if the database is not open. This method is a wrapper around
sqlite3_close_v2()
.Creates and attaches a session to the database. This method is a wrapper around
sqlite3session_create()
andsqlite3session_attach()
.@param optionsThe configuration options for the session.
@returnsA session handle.
- allow: boolean): void;
Enables or disables the
loadExtension
SQL function, and theloadExtension()
method. WhenallowExtension
isfalse
when constructing, you cannot enable loading extensions for security reasons.@param allowWhether to allow loading extensions.
- exec(sql: string): void;
This method allows one or more SQL statements to be executed without returning any results. This method is useful when executing SQL statements read from a file. This method is a wrapper around
sqlite3_exec()
.@param sqlA SQL string to execute.
- name: string,): void;
This method is used to create SQLite user-defined functions. This method is a wrapper around
sqlite3_create_function_v2()
.@param nameThe name of the SQLite function to create.
@param optionsOptional configuration settings for the function.
@param funcThe JavaScript function to call when the SQLite function is invoked. The return value of this function should be a valid SQLite data type: see Type conversion between JavaScript and SQLite. The result defaults to
NULL
if the return value isundefined
.name: string,): void;This method is used to create SQLite user-defined functions. This method is a wrapper around
sqlite3_create_function_v2()
.@param nameThe name of the SQLite function to create.
@param funcThe JavaScript function to call when the SQLite function is invoked. The return value of this function should be a valid SQLite data type: see Type conversion between JavaScript and SQLite. The result defaults to
NULL
if the return value isundefined
. - path: string): void;
Loads a shared library into the database connection. This method is a wrapper around
sqlite3_load_extension()
. It is required to enable theallowExtension
option when constructing theDatabaseSync
instance.@param pathThe path to the shared library to load.
Opens the database specified in the
path
argument of theDatabaseSync
constructor. This method should only be used when the database is not opened via the constructor. An exception is thrown if the database is already open.- sql: string
Compiles a SQL statement into a prepared statement. This method is a wrapper around
sqlite3_prepare_v2()
.@param sqlA SQL string to compile to a prepared statement.
@returnsThe prepared statement.