SqliteDatabase
A native SQLite database connection backed by modernc.org/sqlite (pure Go).
SqliteDatabase provides persistent storage with SQL queries, transactions, prepared statements, migrations, and WAL mode support.
"Open an in-memory database, create a table, insert and query"
| db <SqliteDatabase> row <Dictionary> |
db := SqliteDatabase openMemory.
db execute: 'CREATE TABLE users (id INTEGER PRIMARY KEY, name TEXT, age INTEGER)'.
db execute: 'INSERT INTO users (name, age) VALUES (?, ?)' with: #('Alice' 30).
row := db queryRow: 'SELECT * FROM users WHERE name = ?' with: #('Alice').
row at: 'name'
"File-based database with WAL mode"
| db <SqliteDatabase> |
db := SqliteDatabase open: '/tmp/app.db'.
db enableWAL.
db execute: 'CREATE TABLE IF NOT EXISTS config (key TEXT PRIMARY KEY, val TEXT)'.
db close
Class Methods
primitives
uncategorized
Open a SQLite database at the given file path. Creates the file if it does not exist.
| db <SqliteDatabase> |
db := SqliteDatabase open: '/tmp/myapp.db'.
db close
Open an in-memory SQLite database. Data is lost when the database is closed.
| db <SqliteDatabase> |
db := SqliteDatabase openMemory.
db execute: 'SELECT sqlite_version()'
Instance Methods
primitives
uncategorized
Begin a new transaction. Returns the database for chaining.
db beginTransaction.
db execute: 'INSERT INTO t (val) VALUES (''inside-tx'')'.
db commitTransaction
Close the database connection.
Commit the current transaction.
Enable WAL (Write-Ahead Logging) journal mode for better concurrent read performance.
| db <SqliteDatabase> |
db := SqliteDatabase open: '/tmp/app.db'.
db enableWAL
Execute SQL that returns no rows (CREATE, INSERT, UPDATE, DELETE). Returns the number of rows affected.
| db <SqliteDatabase> |
db := SqliteDatabase openMemory.
db execute: 'CREATE TABLE t (id INTEGER PRIMARY KEY, val TEXT)'.
db execute: 'INSERT INTO t (val) VALUES (''hello'')'
Execute SQL with positional parameters. Parameters are passed as
an Array and bound to ? placeholders in order.
| db <SqliteDatabase> |
db := SqliteDatabase openMemory.
db execute: 'CREATE TABLE t (id INTEGER PRIMARY KEY, name TEXT, age INTEGER)'.
db execute: 'INSERT INTO t (name, age) VALUES (?, ?)' with: #('Alice' 30)
Check if the database connection is closed.
Get the last inserted row ID.
Run a migration SQL if the given version has not been applied. Returns true if the migration was applied, false if already applied.
db migrate: 'CREATE TABLE users (id INTEGER PRIMARY KEY, name TEXT)' version: 1.
db migrate: 'ALTER TABLE users ADD COLUMN age INTEGER' version: 2
Get the highest applied migration version number. Returns 0 if no migrations have been applied.
Get the file path of this database, or ':memory:' for in-memory databases.
Get a PRAGMA value by name.
db pragma: 'journal_mode'
Set a PRAGMA value.
db pragma: 'cache_size' set: '-2000'
Prepare a SQL statement for repeated execution. Returns a SqliteStatement.
| stmt <SqliteStatement> |
stmt := db prepare: 'INSERT INTO users (name, age) VALUES (?, ?)'.
stmt executeWith: #('Alice' 30).
stmt executeWith: #('Bob' 25).
stmt close
Execute a SQL query and return a SqliteRows cursor for iteration.
| db <SqliteDatabase> rows <SqliteRows> |
db := SqliteDatabase openMemory.
db execute: 'CREATE TABLE t (id INTEGER PRIMARY KEY, val TEXT)'.
db execute: 'INSERT INTO t (val) VALUES (''a'')'.
rows := db query: 'SELECT * FROM t'.
[rows next] whileTrue: [rows asDict].
rows close
Execute a SQL query with parameters, returning a SqliteRows cursor.
| rows <SqliteRows> |
rows := db query: 'SELECT * FROM users WHERE age > ?' with: #(25).
[rows next] whileTrue: [rows asDict].
rows close
Execute a query and return all rows as an Array of Dictionaries.
db queryAll: 'SELECT * FROM users ORDER BY name'
Execute a query with parameters and return all rows as an Array of Dictionaries.
db queryAll: 'SELECT * FROM users WHERE age > ?' with: #(25)
Execute a query and return the first row as a Dictionary, or nil if no rows match.
| db <SqliteDatabase> |
db := SqliteDatabase openMemory.
db execute: 'CREATE TABLE t (id INTEGER PRIMARY KEY, name TEXT)'.
db execute: 'INSERT INTO t (name) VALUES (''Alice'')'.
db queryRow: 'SELECT * FROM t WHERE id = 1'
Execute a query with parameters and return the first row as a Dictionary, or nil if no rows match.
db queryRow: 'SELECT * FROM users WHERE name = ?' with: #('Alice')
Rollback the current transaction.
Check if a table exists by name. Returns true or false.
db tableExists: 'users'
List all table names as an Array of Strings.
db tables
Get the SQLite version string.
Execute a block inside a transaction. If the block raises an exception, the transaction is rolled back automatically. Otherwise it is committed.
db withTransaction: [
db execute: 'INSERT INTO users (name) VALUES (?)' with: #('Alice').
db execute: 'INSERT INTO users (name) VALUES (?)' with: #('Bob')
]