Guide

Using Sqliter

Using Sqliter

Add sqliter to the dependencies in pooka.toml.

toml
[dependencies]
sqliter = ">= 1.0.0 and < 2.0.0"

See Platforms and native builds for the package targets and how to compile the library from a source checkout.

Open and query

Use File(path) for a database on disk, Memory() for a private in-memory database, or Temporary() for a temporary database managed by SQLite. defaults() enables foreign keys and sets a five-second busy timeout. with_connection opens the database, runs the callback, and closes it when the callback returns or raises. Its error preserves failures from both the callback and cleanup.

Pooka
module main

import sqliter
import sqliter/error
import sqliter/row
import sqliter/value

class Note(id: Int, title: String)

read_note(db: sqliter.Session, id: Int) -> Result(Note, error.Error) {
  let result = sqliter.query_one(
    db,
    "SELECT id, title FROM notes WHERE id = :id",
    value.Named([#(":id", value.Integer(id))])
  )?
  let id = row.integer(result, 0)?
  let title = row.text(result, 1)?
  Ok(Note(id, title))
}

Bind values with Positional for ? markers or Named for markers such as :id. Include the marker prefix in each name. A parameter represents a value. Column names and other SQL identifiers belong in the SQL statement.

query_one requires one row. query_optional accepts zero or one. For many rows, use query_as with a typed decoder or fold_rows to consume one row at a time. row.nullable preserves SQL NULL as None. Named lookups reject ambiguous aliases. Use row.to_dynamic with the standard library's map decoders when all aliases are unique.

Writes and transactions

execute expects a single statement with no returned rows. For an insert with RETURNING, use a query function. For trusted schema scripts without parameters, use execute_script. A script runs statement by statement unless the caller wraps it in a transaction.

Pooka
module main

import sqliter
import sqliter/value

rename(db: sqliter.Connection, id: Int, title: String) {
  sqliter.transaction(db, sqliter.TransactionMode.Immediate, fn(tx) {
    sqliter.execute(
      tx,
      "UPDATE notes SET title = ? WHERE id = ?",
      value.Positional([value.Text(title), value.Integer(id)])
    )
  })
}

Pass the transaction handle to SQL operations inside the callback. The original connection is reserved until commit or rollback. Returning an error rolls back and preserves the callback error. The transaction owner can use savepoint for nested work. An exception is re-raised after cleanup, and a monitored guardian rolls back when the transaction owner dies.

Use with_statement to reuse a statement and finalize it when the callback returns. fold_rows reserves its connection while the callback runs. Return sqliter.stop(value) to stop early. For writes with RETURNING, use a transaction if stopping early or failing to decode must undo the write.

Connections and snapshots

Use sqliter/database to configure journal and synchronous modes or inspect their effective settings. checkpoint operates on the main database's WAL. backup(source, destination) copies a snapshot between two open connections. restore(destination, snapshot) replaces the destination with the snapshot. Both return the page count.

Pass a fresh cancellation() handle to query_cancellable or execute_cancellable and call cancel from another process. The busy timeout still controls how long SQLite waits for a database lock.