Search results

There are no results.

sqlite.Statement

type pub Statement

A prepared SQL statement to execute.

Instance methods

bind

Show source code
Hide source code
fn pub mut bind[I: BindIndex, V: BindValue](
  column: I,
  value: ref V,
) -> Result[Nil, Error] {
  let col = try bind_column_index(column)

  match value.bind_sqlite_value(@handle, col) {
    case SQLITE_OK -> Result.Ok(nil)
    case v -> throw Error.new(v)
  }
}
fn pub mut bind[I: BindIndex, V: BindValue](column: I, value: ref V) -> Result[Nil, Error]

Binds a value to a column.

column can either be an Int or a String. If an Int, it's treated as the column index starting at index 0 (not at index 1, unlike the SQLite C API).

If a String, it's used as a named SQL parameter. In this case the String must start with :, @ or $ otherwise an Error.InvalidColumn error is produced.

The value argument may be a value of one of the following types:

  • Int (treated as INTEGER)
  • Float (treated as REAL)
  • Bool (treated as INTEGER)
  • Nil (treated as NULL)
  • String (treated as TEXT)
  • ByteArray (treated as BLOB)

Errors

If the column index is out of bounds or otherwise invalid, an Error.InvalidColumn is produced. SQLite may also produce errors for various cases, for more information refer to the SQLite documentation.

Examples

Binding a positional parameter:

import sqlite (Database)

let db = Database.new(':memory:').or_panic
let st = db.prepare('SELECT ?').or_panic

st.bind(column: 0, value: 10) # => Result.Ok(nil)

Binding a named parameter:

import sqlite (Database)

let db = Database.new(':memory:').or_panic
let st = db.prepare('SELECT :value').or_panic

st.bind(column: ':value', value: 10) # => Result.Ok(nil)

bind_blob

Show source code
Hide source code
fn pub mut bind_blob[I: BindIndex](column: I, size: Int) -> Result[Nil, Error]
{
  let col = try bind_column_index(column)
  let len = max(0, size)

  match sqlite3_bind_zeroblob64(@handle, col as Int32, len as Uint64) as Int {
    case SQLITE_OK -> Result.Ok(nil)
    case v -> throw Error.new(v)
  }
}
fn pub mut bind_blob[I: BindIndex](column: I, size: Int) -> Result[Nil, Error]

Binds a blob with size zeroes to a column.

If size is less than zero, the blob is empty as if the size were zero instead.

This method is meant to be used for creating a placeholder BLOB value that is later updated by acquiring a Blob through Database.blob, as SQLite offers no API to resize existing blobs.

Refer to the documentation of Statement.bind for more details.

Examples

import sqlite (Database)

let db = Database.new(':memory:').or_panic
let st = db.prepare('SELECT ?').or_panic

st.bind_blob(column: 0, size: 32) # => Result.Ok(nil)

execute

Show source code
Hide source code
fn pub mut execute -> Result[Int, Error] {
  match sqlite3_step(@handle) as Int {
    case SQLITE_ROW or SQLITE_DONE -> Result.Ok(@db.changes)
    case v -> throw Error.new(v)
  }
}
fn pub mut execute -> Result[Int, Error]

Executes the statement and returns the number of modified (e.g. inserted) rows.

Examples

import sqlite (Database)

let db = Database.new(':memory:').or_panic
let _ = db.execute('CREATE TABLE users (name TEXT)').or_panic
let st = db.prepare('INSERT INTO users VALUES ("Alice")').or_panic

st.execute # => Result.Ok(1)

number_of_columns

Show source code
Hide source code
fn pub inline number_of_columns -> Int {
  sqlite3_column_count(@handle) as Int
}
fn pub inline number_of_columns -> Int

Returns the number of columns produced by this statement.

Examples

import sqlite (Database)

let db = Database.new(':memory:').or_panic
let st = db.prepare('SELECT 1').or_panic

st.number_of_columns # => 1

reset

Show source code
Hide source code
fn pub mut reset -> Result[Nil, Error] {
  match sqlite3_reset(@handle) as Int {
    case SQLITE_OK -> sqlite3_clear_bindings(@handle)
    case v -> throw Error.new(v)
  }

  Result.Ok(nil)
}
fn pub mut reset -> Result[Nil, Error]

Resets the statement to its initial state and clears all bound values.

Errors

This method may return an Error based on the result of the last call to Statement.execute or Rows.next?. For more details, refer to the SQLite documentation.

Examples

import sqlite (Database)

let db = Database.new(':memory:').or_panic
let st = db.prepare('SELECT 1').or_panic

st.reset # => Result.Ok(nil)

rows

Show source code
Hide source code
fn pub inline mut rows -> Rows {
  Rows(self)
}
fn pub inline mut rows -> Rows

Returns an iterator over the rows produced by this statement.

The statement is not executed until the first call to Rows.next.

Examples

import sqlite (Database)

let db = Database.new(':memory:').or_panic
let st = db.prepare('SELECT 1').or_panic

st.rows.count # => 1

Implemented traits

std.drop.

Drop

impl Drop for Statement