title: Database description: It is the core object of the SQLite API: all SQL execution, querying, schema inspection, transaction handling, and savepoint management are performed through Database.
Database represents a concrete database access context.
It is the core object of the SQLite API: all SQL execution, querying, schema inspection, transaction handling, and savepoint management are performed through Database.
In Scripting, a Database instance is never created directly.
It is provided by DatabaseQueue or DatabasePool inside read / write callbacks.
Type Definition
Role and Responsibilities
Database is responsible for:
- Executing SQL statements (DDL and DML)
- Creating and managing
Statementinstances - Querying and fetching data
- Exposing schema metadata
- Managing transactions and savepoints
Database is not responsible for:
- Concurrency scheduling (handled by Queue / Pool)
- Lifecycle management (handled by Queue / Pool)
- Cross-callback or cross-thread reuse
Obtaining a Database Instance
A Database instance is only available inside DatabaseQueue or DatabasePool callbacks.
State Properties
changesCount
Returns the number of rows affected by the most recent SQL statement.
totalChangesCount
Returns the total number of rows affected since the database connection was opened.
isInsideTransaction
Indicates whether the database is currently inside a transaction or savepoint.
lastErrorMessage
Returns the last SQLite error message, if any.
lastInsertedRowID
Returns the row ID generated by the most recent insert operation.
Schema Version
schemaVersion
Returns the current schema version of the database.
This is commonly used in migration logic:
Schema Inspection APIs
tableExists
Checks whether a table exists.
isTableHasUniqueKeys
Checks whether a table defines a unique constraint on the specified column set.
columnsIn
Returns column metadata for a table.
primaryKey / foreignKeys / indexes
Returns primary key, foreign key, and index metadata.
Statement Creation
makeStatement
Creates a new, non-cached Statement.
cachedStatement
Creates or retrieves a cached Statement.
Transactions and Savepoints
inTransaction
Executes a block inside a transaction.
inSavepoint
Executes a block inside a savepoint. Savepoints may be nested.
Notes:
- Return
"commit"to commit - Return
"rollback"to roll back - Throwing an error automatically rolls back
Executing SQL
execute
Executes an SQL statement without returning rows.
Suitable for:
- DDL statements
- INSERT / UPDATE / DELETE
- Statements where no result set is required
Table and Index Management
createTable
Creates a table using structured column definitions.
renameTable / dropTable
createIndex
dropIndex / dropIndexOn
Query APIs
fetchAll
Returns all rows.
fetchSet
Returns a set-like result with duplicate elimination.
fetchOne
Returns a single row.
fetchCursor
Reads rows incrementally using a cursor.
Suitable for:
- Large result sets
- Streaming-style processing
- Avoiding loading all rows into memory
Usage Constraints and Notes
Databasemust only be used within its owningread/writecallback- Do not store or pass
Databaseinstances outside the callback - Do not use across threads
- Transactions must complete within the same
Databasecontext
Common Mistakes
- Retaining a
Databasereference outside the callback - Executing transaction-dependent logic outside a transaction
- Using
fetchCursorunnecessarily for small queries - Mixing Queue and Pool semantics incorrectly
Summary
Database is the core execution unit of the SQLite API:
- Provides full SQL execution and querying capabilities
- Manages statements, transactions, and schema access
- Enforces clear concurrency and lifecycle boundaries
- Works in tandem with Queue and Pool for safe access
