v1.2.2 — 2026-06-07
SQLite3 database access.
# Debian/Ubuntu
sudo apt install libsqlite3-dev
# macOS
brew install sqliteEnabled by -DBUILD_MODULE_SQLITE=ON (default). Skipped silently if SQLite3 is not found.
(import (curry sqlite))(sqlite-open path) ; open or create a database file
(sqlite-open-memory) ; open an in-memory database
(sqlite-close db) ; close the database(sqlite-exec db sql) ; execute SQL; returns list of rows (each row is a list)sqlite-exec is convenient for DDL and simple queries. Each row is a list of values; column values are strings, integers, or #f for NULL.
(sqlite-prepare db sql) ; compile SQL, return statement handle
(sqlite-bind stmt index val); bind parameter (1-based index)
(sqlite-step stmt) ; step: returns next row as list, or #f when done
(sqlite-finalize stmt) ; release the statementBind values: string, integer, flonum, or #f (NULL). Parameters in SQL are ? or ?N.
(sqlite-last-insert-rowid db) ; integer rowid of last INSERT
(sqlite-changes db) ; number of rows changed by last statement(import (curry sqlite))
; Create and populate
(define db (sqlite-open "/tmp/test.db"))
(sqlite-exec db "CREATE TABLE IF NOT EXISTS people (id INTEGER PRIMARY KEY, name TEXT, age INTEGER)")
(sqlite-exec db "INSERT INTO people (name, age) VALUES ('Alice', 30)")
(sqlite-exec db "INSERT INTO people (name, age) VALUES ('Bob', 25)")
; Simple query
(sqlite-exec db "SELECT * FROM people")
; => (("1" "Alice" "30") ("2" "Bob" "25"))
; Prepared statement with parameters
(define stmt (sqlite-prepare db "SELECT name FROM people WHERE age > ?"))
(sqlite-bind stmt 1 27)
(let loop ((row (sqlite-step stmt)))
(when row
(display (car row)) (newline)
(loop (sqlite-step stmt))))
(sqlite-finalize stmt)
(sqlite-close db)(sqlite-exec db "BEGIN")
(sqlite-exec db "INSERT INTO ...")
(sqlite-exec db "UPDATE ...")
(sqlite-exec db "COMMIT")
; or (sqlite-exec db "ROLLBACK") on error- All column values from
sqlite-execandsqlite-stepare Scheme strings (SQLite returns text by default via the C API). Cast as needed:(string->number (car row)). - For high-performance bulk inserts, use a prepared statement inside a transaction.
- In-memory databases are lost when the handle is closed.