Lynxer

Documentation

SQLite Database Module

The sqldb module provides SQLite database functionality using Rust's rusqlite crate.

Functions

Query output uses JSON-native null, numbers and strings. BLOB columns use a tagged object such as {"$bytes": "AAH/gA=="}; the tag preserves their byte identity and the base64 payload preserves arbitrary bytes. Pass the same single-key object in paramsJson to bind those bytes back as a SQLite BLOB.

scalar() keeps the reference-compatible string result: NULL and no row return "", numeric values use their decimal text, text is unchanged, and BLOBs use bytes:<base64> so they cannot be mistaken for ordinary text.

Connection handles

Every function above names a path and opens a connection for that call only. To reuse one connection, open it once and use the *On forms with the handle:

Handles stay valid for the run, and a closed or unknown handle answers the usual sentinels (-1, false) for integer- and boolean-returning operations. String-returning operations raise a catchable runtime error, including for an unknown or closed handle. Two threads can share a handle: each call takes the connection for its duration.

Catch database failures with the language's try/catch statement:

try {
    global.sqldb.query("database.db", "SELECT * FROM missing_table");
} catch(str error) {
    println("Database error: ", error);
}

Example

global setup(){
    import("sqldb")
}

global main(){
    global.sqldb.execute("database.db", "CREATE TABLE users (id INTEGER PRIMARY KEY, name TEXT)");
    global.sqldb.execute("database.db", "INSERT INTO users (name) VALUES ('Alice')");
    println(global.sqldb.query("database.db", "SELECT * FROM users"));

    // Or keep one connection open across calls.
    int db = global.sqldb.open("database.db");
    global.sqldb.executeOn(db, "INSERT INTO users (name) VALUES ('Bob')");
    println(global.sqldb.queryOn(db, "SELECT * FROM users ORDER BY id"));
    global.sqldb.close(db);
}

See also