Statement
Statement represents a prepared SQL statement.
It is designed to improve performance for repeated execution, ensure safe parameter binding, and provide explicit argument validation for complex workflows.
In the Scripting SQLite API, Statement instances are created and managed by Database.
They cannot be constructed directly by users.
Type Definition
StatementArguments
StatementArguments defines the supported parameter binding formats for Statement and SQL execution.
It is the unified argument model used throughout the SQLite API.
Supported Forms
Positional Arguments
Use ? placeholders, with values bound by position.
Characteristics:
- Argument order must match the order of
?placeholders - Simple and compact
- Less suitable for complex or long-lived SQL
Named Arguments
Use named placeholders such as :name.
Characteristics:
- Clear semantics
- Order-independent
- Recommended for complex or maintainable SQL
DatabaseValue Constraints
All values in StatementArguments must conform to DatabaseValue:
Unsupported types (for example objects, arrays, or functions) will cause validation or execution failures.
Design Goals and Use Cases
The core goals of Statement are:
- Reuse compiled SQL to reduce parsing overhead
- Centralize argument binding logic
- Provide pre-execution argument validation
- Improve performance and reliability
Recommended use cases:
- Repeated execution of the same SQL
- Batch or loop-based operations
- Performance-critical paths
- APIs that require strict argument correctness
For one-off SQL execution, Database.execute is usually sufficient.
Creating Statements
makeStatement
Creates a new Statement instance on each call.
Use when:
- Execution frequency is low
- Statement lifetime is short
- Caching is unnecessary
cachedStatement
Returns a cached and reusable Statement. The same SQL is compiled only once per database instance.
Use when:
- SQL is executed frequently
- Batch inserts or updates are performed
- Performance is important
Properties
sql
Returns the original SQL string associated with the statement.
columnNames
Returns the column names of the result set.
Notes:
- Only meaningful for
SELECTstatements - Useful for dynamic mapping, debugging, or generic query helpers
isReadonly
Indicates whether the statement is read-only.
Notes:
SELECTstatements are typically read-onlyINSERT,UPDATE, andDELETEare not- Can be used for permission checks or pre-execution validation
Argument Binding and Execution Flow
execute
Executes the statement, optionally binding arguments at execution time.
Or execute using previously bound arguments:
Rules:
- Arguments passed to
executeoverride previously set arguments - If no arguments are passed, the most recent
setArgumentsvalue is used
setArguments
Binds arguments to the statement with full validation, without executing it.
Use when:
- Argument preparation and execution are separated
- The same arguments are reused across multiple steps
- Early validation is desirable
validateArguments
Validates arguments without executing the statement.
Validation includes:
- Missing required arguments
- Mismatch between named placeholders and keys
- Incorrect argument count
- Invalid
DatabaseValuetypes
Common use cases:
- Pre-flight validation
- Building stricter higher-level APIs
- Providing clearer error messages before execution
setUncheckedArguments
Binds arguments without validation.
Notes:
- Faster than validated binding
- Errors surface only during execution
- Use only when argument correctness is guaranteed
Relationship to Database.execute
The following approaches are functionally equivalent but serve different purposes:
Guidelines:
Lifecycle and Usage Constraints
- A
Statementis owned by theDatabasethat created it - Statements must not be used after the database is closed
- Statements must not be shared across different databases
- Statements are not thread-safe
They must be used within their owning
read/writecallback
Common Mistakes
- Reusing a statement across multiple databases
- Overusing
Statementfor one-off SQL - Excessive use of
setUncheckedArguments - Executing statements after the database has been closed
- Passing values that are not valid
DatabaseValuetypes
Summary
Statement is a core component of the SQLite API for high-performance, strongly constrained SQL execution:
- Clear separation between SQL compilation and execution
- Unified argument model via
StatementArguments - Strict and non-strict argument binding options
- Well-defined responsibilities alongside
Database.execute
For scenarios that require performance, maintainability, and argument safety, Statement is the preferred tool.
