libstd/sqlite3

Bindings to SQLite plus small helpers: open a database, run statements with error checking, prepared statements with text parameters. It links libsqlite3.so on Linux and winsqlite3.dll on Windows, the SQLite that ships with Windows 10 and later.

#include <libstd/sqlite3>

Includes libstd/stdio. Links the shared library libsqlite3.so, winsqlite3.dll (FFI).

Example

#include <libstd/stdio>
#include <libstd/sqlite3>

db =: sqlite3
rc = sqlite3_open("./test.db", &db)
sqlite3_exec_w(db, "CREATE TABLE IF NOT EXISTS Users(valore INTEGER);")
i = 0
while i < 3:
    sqlite3_exec_w(db, "INSERT INTO Users(valore) VALUES(" + itoa(i) + ");")
    i++
sqlite3_close(db)
print(sqlite3_libversion(), "\n")

Constants

NameValueMeaning
SQLITE_OK0
SQLITE_ERROR1
SQLITE_INTERNAL2
SQLITE_PERM3
SQLITE_ABORT4
SQLITE_BUSY5
SQLITE_LOCKED6
SQLITE_NOMEM7
SQLITE_READONLY8
SQLITE_INTERRUPT9
SQLITE_IOERR10
SQLITE_CORRUPT11
SQLITE_NOTFOUND12
SQLITE_FULL13
SQLITE_CANTOPEN14
SQLITE_TRANSIENT-1
SQLITE_DONE101
SQLITE_ROW100

Functions

extern def sqlite3_libversion() -> str

Version string of the library.

extern def sqlite3_open(filename: str, ppDb: &sqlite3) -> int

Opens (or creates) a database file; writes the handle through ppDb. Returns SQLITE_OK on success.

extern def sqlite3_exec(dbs: sqlite3, sql: str, callback: int, arg: int, errmsg: &str) -> int

Runs one or more SQL statements with an optional row callback.

extern def sqlite3_close(dbs: sqlite3) -> int

Closes a database handle.

extern def sqlite3_free(ptr: str) -> None

Frees memory allocated by SQLite (error messages).

extern def sqlite3_prepare_v2(dbs: sqlite3, SQL: str, nBytes: int, stmt: &sqlite3_stmt, ptr: int) -> int

Compiles an SQL statement into a sqlite3_stmt.

extern def sqlite3_bind_text(stmt: sqlite3_stmt, index: int, text: str, len_text: int, flag: int) -> int

Binds a text value to a ? placeholder (1-based index).

extern def sqlite3_step(stmt: sqlite3_stmt) -> int

Executes a prepared statement one step; returns SQLITE_ROW or SQLITE_DONE.

extern def sqlite3_finalize(stmt: sqlite3_stmt) -> int

Destroys a prepared statement.

def sqlite3_checker_error(dbs: sqlite3, response: int, error: str) -> int

If response is not SQLITE_OK, prints error, closes the database and exits. Call after every SQLite call whose failure is fatal.

def response_sqlite3_query(arg: int, argc: int, argv: str, azColName: int) -> int

A callback with the signature sqlite3_exec expects; a template to extend when rows must be read.

def sqlite3_exec_w(dbs: sqlite3, query: str) -> int

Runs a statement that returns no rows (INSERT/UPDATE/CREATE ...) with automatic error checking.

def sqlite3_drop_table(dbs: sqlite3, name_table: str) -> int
DROP TABLE name.
def sqlite3_run_query(dbs: sqlite3) -> None

Runs a sample SELECT * FROM Users; through response_sqlite3_query; a starting point for real queries.

def sqlite3_prepare_stmnt(dbs: sqlite3, sql: str, stmt: &sqlite3_stmt) -> int

Prepares a parameterised statement (? placeholders) for sqlite3_bind and sqlite3_step.

def sqlite3_bind(stmt: sqlite3_stmt, index: int, text: str) -> int

Binds a text value to placeholder index (1-based) of a prepared statement.