Statement
public final class Statement
extension Statement: Sequence
extension Statement: IteratorProtocol
A compiled SQL statement with support for SQL parameter binding and result row processing.
Creation
A statement is not created directly but is obtained from a Database.
let statement = try db.prepare(sql: "select count(*) from t1;")
Parameter Binding
A statement supports binding values to SQL parameters by index or by name.
Note
Parameter indexes are 1-based. The leftmost parameter in a statement has index 1.let statement = try db.prepare(sql: "insert into t1(a, b, c, d, e, f) values (?, ?, ?, :d, :e, :f);")
try statement.bind(value: 30, toParameter: 3)
try statement.bind(value: 40, toParameter: ":d")
try statement.bind(parameterValues: 10, 20)
try statement.bind(parameters: [":f": 60, ":e": 50])
Result Rows
When executed a statement provides zero or more result rows.
try statement.results { row in
// Do something with `row`
}
for row in statement {
// Do something with `row`
}
It is generally preferred to use the block-based method because any errors may be explicitly handled instead of silently discarded.
-
The owning database
Declaration
Swift
public let database: Database -
trueif this statement makes no direct changes to the database,falseotherwise.Seealso
Read-only statements in SQLiteDeclaration
Swift
public lazy var isReadOnly: Bool { get set } -
The number of SQL parameters in this statement
Declaration
Swift
public lazy var parameterCount: Int { get set } -
The original SQL text of the statement
Declaration
Swift
public lazy var sql: String { get set } -
The SQL text of the statement with bound parameters expanded
Declaration
Swift
public var expandedSQL: String { get } -
The number of columns in the result set
Declaration
Swift
public lazy var columnCount: Int { get set } -
Returns the name of the column at
index.Note
Column indexes are 0-based. The leftmost column in a result row has index 0.
Requires
index >= 0Requires
index < self.columnCountThrows
An error if
indexis out of boundsDeclaration
Swift
public func name(ofColumn index: Int) throws -> StringParameters
indexThe index of the desired column
Return Value
The name of the column for the specified index
-
Returns the index of the column
name.Throws
An error if the column doesn’t exist
Declaration
Swift
public func index(ofColumn name: String) throws -> IntParameters
nameThe name of the desired column
Return Value
The index of the column with the specified name
-
Executes the statement.
Requires
The statement does not return any result rows
Throws
An error if the statement returned any result rows or did not successfully run to completion
Declaration
Swift
public func execute() throws -
Executes the statement and applies
blockto each result row.Throws
Any error thrown in
blockor an error if the statement did not successfully run to completionDeclaration
Swift
public func results(_ block: ((_ row: Row) throws -> ())) throwsParameters
blockA closure applied to each result row
rowA result row of returned data
-
Returns the next result row or
nilif none.Throws
An error if the statement encountered an execution error
Declaration
Swift
public func nextRow() throws -> Row?Return Value
The next result row of returned data
-
Resets the statement to its initial state, ready to be re-executed.
Note
This function does not change the value of any bound SQL parameters.
Throws
An error if the statement could not be reset
Declaration
Swift
public func reset() throws -
Clears all statement bindings by setting SQL parameters to null.
Throws
An error if the bindings could not be clearedDeclaration
Swift
public func clearBindings() throws -
Performs a low-level SQLite statement operation.
Use of this function should be avoided whenever possible
Throws
Any error thrown in
blockDeclaration
Swift
public func withUnsafeRawSQLiteStatement<T>(block: (_ stmt: SQLitePreparedStatement) throws -> (T)) rethrows -> TParameters
blockA closure performing the operation
stmtThe raw
sqlite3_stmt *statement objectReturn Value
The value returned by
block -
Returns the values of the columns at
indexesfor each row in the result set.Note
Column indexes are 0-based. The leftmost column in a row has index 0.
Requires
indexes.min() >= 0Requires
indexes.max() < self.columnCountThrows
An error if any element of
indexesis out of boundsDeclaration
Swift
public func columns<S, T>(_ indexes: S) throws -> [[T]] where S : Collection, T : ColumnConvertible, S.Element == IntParameters
indexesThe indexes of the desired columns
-
Returns the value of the column at
indexfor each row in the result set.Note
Column indexes are 0-based. The leftmost column in a row has index 0.
Requires
index >= 0Requires
index < self.columnCountThrows
An error if
indexis out of bounds or the column contains a null or illegal valueDeclaration
Swift
public func column<T>(_ index: Int) throws -> [T] where T : ColumnConvertibleParameters
indexThe index of the desired column
-
Returns the value of the leftmost column for each row in the result set.
This is a shortcut for
column(0).Throws
An error if there are no columns
Declaration
Swift
public func leftmostColumn<T>() throws -> [T] where T : ColumnConvertibleReturn Value
An array containing the leftmost column’s values
-
Returns the value of the leftmost column in the first row.
Throws
An error if there are no columns or the column contains an illegal value
Declaration
Swift
public func front<T>() throws -> T? where T : ColumnConvertibleReturn Value
The value of the leftmost column in the first row
-
Returns the value of the leftmost column in the first row.
Throws
An error if there are no rows, no columns, or the column contains a null or illegal value
Declaration
Swift
public func front<T>() throws -> T where T : ColumnConvertibleReturn Value
The value of the leftmost column in the first row
-
Returns the values of the columns at
indexesfor each row in the result set.Note
Column indexes are 0-based. The leftmost column in a row has index 0.
Requires
indexes.min() >= 0Requires
indexes.max() < self.columnCountThrows
An error if any element of
indexesis out of boundsDeclaration
Swift
public func columns<S, T>(_ indexes: S) throws -> [[T]] where S : Collection, T : DatabaseSerializable, S.Element == IntParameters
indexesThe indexes of the desired columns
-
Returns the value of the column at
indexfor each row in the result set.Note
Column indexes are 0-based. The leftmost column in a row has index 0.
Requires
index >= 0Requires
index < self.columnCountThrows
An error if
indexis out of bounds or the column contains a null or illegal valueDeclaration
Swift
public func column<T>(_ index: Int) throws -> [T] where T : DatabaseSerializableParameters
indexThe index of the desired column
-
Returns the value of the leftmost column for each row in the result set.
This is a shortcut for
column(0).Throws
An error if there are no columns
Declaration
Swift
public func leftmostColumn<T>() throws -> [T] where T : DatabaseSerializableReturn Value
An array containing the leftmost column’s values
-
Returns the value of the leftmost column in the first row.
Throws
An error if there are no columns or the column contains an illegal value
Declaration
Swift
public func front<T>() throws -> T? where T : DatabaseSerializableReturn Value
The value of the leftmost column in the first row
-
Returns the value of the leftmost column in the first row.
Throws
An error if there are no rows, no columns, or the column contains an illegal value
Declaration
Swift
public func front<T>() throws -> T where T : DatabaseSerializableReturn Value
The value of the leftmost column in the first row
-
Returns the values of the columns at
indexesfor each row in the result set.Note
Column indexes are 0-based. The leftmost column in a row has index 0.
Requires
indexes.min() >= 0Requires
indexes.max() < self.columnCountThrows
An error if any element of
indexesis out of boundsDeclaration
Swift
public func columns<S>(_ indexes: S) throws -> [[DatabaseValue]] where S : Collection, S.Element == IntParameters
indexesThe indexes of the desired columns
-
Returns the value of the column at
indexfor each row in the result set.Note
Column indexes are 0-based. The leftmost column in a row has index 0.
Requires
index >= 0Requires
index < self.columnCountThrows
An error if
indexis out of boundsDeclaration
Swift
public func column(_ index: Int) throws -> [DatabaseValue]Parameters
indexThe index of the desired column
-
Returns the value of the leftmost column for each row in the result set.
This is a shortcut for
column(0).Throws
An error if there are no columns
Declaration
Swift
public func leftmostColumn() throws -> [DatabaseValue]Return Value
An array containing the leftmost column’s values
-
Returns the value of the leftmost column in the first result row.
Throws
An error if there are no columns
Declaration
Swift
public func front() throws -> DatabaseValue?Return Value
The value of the leftmost column in the first result row
-
Returns the value of the leftmost column in the first result row.
Throws
An error if there are no rows or columns
Declaration
Swift
public func front() throws -> DatabaseValueReturn Value
The value of the leftmost column in the first result row
-
Binds database
NULLto the SQL parameter atindex.Note
Parameter indexes are 1-based. The leftmost parameter in a statement has index 1.
Requires
index > 0Requires
index < parameterCountThrows
An error if
NULLcouldn’t be boundDeclaration
Swift
public func bindNull(toParameter index: Int) throwsParameters
indexThe index of the SQL parameter to bind
-
Binds database
NULLto the SQL parametername.Throws
An error if the SQL parameter
namedoesn’t exist orNULLcouldn’t be boundDeclaration
Swift
public func bindNull(toParameter name: String) throwsParameters
nameThe name of the SQL parameter to bind
-
Binds
valueto the SQL parameter atindex.Note
Parameter indexes are 1-based. The leftmost parameter in a statement has index 1.
Requires
index > 0Requires
index < parameterCountThrows
An error if
valuecouldn’t be boundDeclaration
Swift
public func bind<T>(value: T, toParameter index: Int) throws where T : ParameterBindableParameters
valueThe desired value of the SQL parameter
indexThe index of the SQL parameter to bind
-
Binds
valueto the SQL parameter atindex.Note
Parameter indexes are 1-based. The leftmost parameter in a statement has index 1.
Requires
index > 0Requires
index < parameterCountThrows
An error if
valuecouldn’t be boundDeclaration
Swift
public func bind<T>(value: T?, toParameter index: Int) throws where T : ParameterBindableParameters
valueThe desired value of the SQL parameter
indexThe index of the SQL parameter to bind
-
Binds
valueto the SQL parametername.Throws
An error if the SQL parameter
namedoesn’t exist orvaluecouldn’t be boundDeclaration
Swift
public func bind<T>(value: T, toParameter name: String) throws where T : ParameterBindableParameters
valueThe desired value of the SQL parameter
nameThe name of the SQL parameter to bind
-
Binds
valueto the SQL parametername.Throws
An error if the SQL parameter
namedoesn’t exist orvaluecouldn’t be boundDeclaration
Swift
public func bind<T>(value: T?, toParameter name: String) throws where T : ParameterBindableParameters
valueThe desired value of the SQL parameter
nameThe name of the SQL parameter to bind
-
Binds the n parameters in
valuesto the first n SQL parameters ofself.Throws
An error if one of
valuescouldn’t be boundDeclaration
Swift
@discardableResult public func bind<C>(parameterValues values: C) throws -> Statement where C : Collection, C.Element : ParameterBindableParameters
valuesA collection of values to bind to SQL parameters
Return Value
self -
Binds value to SQL parameter name for each (name, value) in
parameters.Throws
An error if the SQL parameter name doesn’t exist or value couldn’t be bound
Declaration
Swift
@discardableResult public func bind<C>(parameters: C) throws -> Statement where C : Collection, C.Element == (String, V: ParameterBindable)Parameters
parametersA collection of name and value pairs to bind to SQL parameters
Return Value
self -
Binds the n parameters in
valuesto the first n SQL parameters ofself.Requires
values.count <= self.parameterCountThrows
An error if one of
valuescouldn’t be boundDeclaration
Swift
@discardableResult public func bind(parameterValues values: [ParameterBindable?]) throws -> StatementParameters
valuesA series of values to bind to SQL parameters
Return Value
self -
Binds value to SQL parameter name for each (name, value) in
parameters.Throws
An error if the SQL parameter name doesn’t exist or value couldn’t be bound
Declaration
Swift
@discardableResult public func bind(parameters: [String : ParameterBindable?]) throws -> StatementParameters
parametersA series of name and value pairs to bind to SQL parameters
Return Value
self
-
Binds the values in
arrayto the SQL parameter atindexusing the sqlite3 Carray extensionWhen using the Carray extension in
FeistyDBthe number of array elements must be explicitly bound:let primes = [ 3, 5, 7 ] let statement = try db.prepare(sql: "SELECT * FROM numbers WHERE value IN carray(?1,?2,'int32');") try statement.bind(array: primes, toParameter: 1) try statement.bind(value: primes.count, toParameter: 2)Note
Parameter indexes are 1-based. The leftmost parameter in a statement has index 1.
Requires
index > 0Requires
index < parameterCountThrows
An error if
arraycouldn’t be boundDeclaration
Swift
public func bind<S>(array: S, toParameter index: Int) throws where S : Collection, S.Element == Int32Parameters
arrayAn array of values to bind to the SQL parameter
indexThe index of the SQL parameter to bind
-
Binds the values in
arrayto SQL parameternameusing the sqlite3 Carray extensionWhen using the Carray extension in
FeistyDBthe number of array elements must be explicitly bound:let primes = [ 3, 5, 7 ] let statement = try db.prepare(sql: "SELECT * FROM numbers WHERE value IN carray(:primes,?2,'int32');") try statement.bind(array: primes, toParameter: ":primes") try statement.bind(value: primes.count, toParameter: 2)Throws
An error if the SQL parameter
namedoesn’t exist orarraycouldn’t be boundDeclaration
Swift
public func bind<S>(array: S, toParameter name: String) throws where S : Collection, S.Element == Int32Parameters
arrayAn array of values to bind to the SQL parameter
nameThe name of the SQL parameter to bind
-
Binds the values in
arrayto the SQL parameter atindexusing the sqlite3 Carray extensionWhen using the Carray extension in
FeistyDBthe number of array elements must be explicitly bound:let primes = [ 87178291199, 99194853094755497 ] let statement = try db.prepare(sql: "SELECT * FROM numbers WHERE value IN carray(?1,?2,'int64');") try statement.bind(array: primes, toParameter: 1) try statement.bind(value: primes.count, toParameter: 2)Note
Parameter indexes are 1-based. The leftmost parameter in a statement has index 1.
Requires
index > 0Requires
index < parameterCountThrows
An error if
arraycouldn’t be boundDeclaration
Swift
public func bind<S>(array: S, toParameter index: Int) throws where S : Collection, S.Element == Int64Parameters
arrayAn array of values to bind to the SQL parameter
indexThe index of the SQL parameter to bind
-
Binds the values in
arrayto SQL parameternameusing the sqlite3 Carray extensionWhen using the Carray extension in
FeistyDBthe number of array elements must be explicitly bound:let primes = [ 87178291199, 99194853094755497 ] let statement = try db.prepare(sql: "SELECT * FROM numbers WHERE value IN carray(:primes,?2,'int64');") try statement.bind(array: primes, toParameter: ":primes") try statement.bind(value: primes.count, toParameter: 2)Throws
An error if the SQL parameter
namedoesn’t exist orarraycouldn’t be boundDeclaration
Swift
public func bind<S>(array: S, toParameter name: String) throws where S : Collection, S.Element == Int64Parameters
arrayAn array of values to bind to the SQL parameter
nameThe name of the SQL parameter to bind
-
Binds the values in
arrayto the SQL parameter atindexusing the sqlite3 Carray extensionWhen using the Carray extension in
FeistyDBthe number of array elements must be explicitly bound:let specials = [ Double.pi, Double.nan, Double.infinity ] let statement = try db.prepare(sql: "SELECT * FROM numbers WHERE value IN carray(?1,?2,'double');") try statement.bind(array: specials, toParameter: 1) try statement.bind(value: specials.count, toParameter: 2)Note
Parameter indexes are 1-based. The leftmost parameter in a statement has index 1.
Requires
index > 0Requires
index < parameterCountThrows
An error if
arraycouldn’t be boundDeclaration
Swift
public func bind<S>(array: S, toParameter index: Int) throws where S : Collection, S.Element == DoubleParameters
arrayAn array of values to bind to the SQL parameter
indexThe index of the SQL parameter to bind
-
Binds the values in
arrayto SQL parameternameusing the sqlite3 Carray extensionWhen using the Carray extension in
FeistyDBthe number of array elements must be explicitly bound:let specials = [ Double.pi, Double.nan, Double.infinity ] let statement = try db.prepare(sql: "SELECT * FROM numbers WHERE value IN carray(:specials,?2,'double');") try statement.bind(array: specials, toParameter: ":specials") try statement.bind(value: specials.count, toParameter: 2)Throws
An error if the SQL parameter
namedoesn’t exist orarraycouldn’t be boundDeclaration
Swift
public func bind<S>(array: S, toParameter name: String) throws where S : Collection, S.Element == DoubleParameters
arrayAn array of values to bind to the SQL parameter
nameThe name of the SQL parameter to bind
-
Binds the values in
arrayto the SQL parameter atindexusing the sqlite3 Carray extensionWhen using the Carray extension in
FeistyDBthe number of array elements must be explicitly bound:let pets = [ "dog", "dragon", "hedgehog" ] let statement = try db.prepare(sql: "SELECT * FROM animals WHERE kind IN carray(?1,?2,'char*');") try statement.bind(array: pets, toParameter: 1) try statement.bind(value: pets.count, toParameter: 2)Note
Parameter indexes are 1-based. The leftmost parameter in a statement has index 1.
Requires
index > 0Requires
index < parameterCountThrows
An error if
arraycouldn’t be boundDeclaration
Swift
public func bind<S>(array: S, toParameter index: Int) throws where S : Collection, S.Element == StringParameters
arrayAn array of values to bind to the SQL parameter
indexThe index of the SQL parameter to bind
-
Binds the values in
arrayto SQL parameternameusing the sqlite3 Carray extensionWhen using the Carray extension in
FeistyDBthe number of array elements must be explicitly bound:let pets = [ "dog", "dragon", "hedgehog" ] let statement = try db.prepare(sql: "SELECT * FROM animals WHERE kind IN carray(:pets,?2,'char*');") try statement.bind(array: pets, toParameter: ":pets") try statement.bind(value: pets.count, toParameter: 2)Throws
An error if the SQL parameter
namedoesn’t exist orarraycouldn’t be boundDeclaration
Swift
public func bind<S>(array: S, toParameter name: String) throws where S : Collection, S.Element == StringParameters
arrayAn array of values to bind to the SQL parameter
nameThe name of the SQL parameter to bind
-
Returns the first result row or
nilif none.Throws
An error if the statement encountered an execution error
Declaration
Swift
public func firstRow() throws -> Row?Return Value
The first result row
-
Returns an iterator for accessing the result rows.
Because the iterator discards errors, the preferred way of accessing result rows is via
nextRow()orresults(_:)Declaration
Swift
public func makeIterator() -> StatementReturn Value
An iterator over the result rows
-
Returns the next result row or
nilif none.Because the iterator discards errors, the preferred way of accessing result rows is via
nextRow()orresults(_:)Declaration
Swift
public func next() -> Row?Return Value
The next result row of returned data
-
Declaration
Swift
public enum Counter -
Returns information on a statement counter.
Seealso
Declaration
Swift
public func count(of counter: Counter, reset: Bool = false) -> IntParameters
counterThe desired statement counter
resetIf
truethe counter is reset to zeroReturn Value
The current value of the counter
View on GitHub
Statement Class Reference