55 / Networking

SQLite

std/sqlite provides SQLite3 bindings loaded at runtime via dlopen. sqlite_open(":memory:") creates an in-memory database, sqlite_exec runs DDL/DML statements, and sqlite_query returns rows as arrays of strings. No external dependencies are needed — SQLite is bundled with most operating systems.

55-sqlite.nxSource →
// SQLite — in-memory database with SQL queries
// SQLite — base de datos en memoria con consultas SQL

import "std/sqlite"

fn main() -> int {
    // Open an in-memory database. The handle is an opaque pointer (*int);
    // sqlite_open returns NULL on failure.
    let db = sqlite_open(":memory:")

    // Create a table
    sqlite_exec(db, "CREATE TABLE users (id INTEGER PRIMARY KEY, name TEXT, age INTEGER)")

    // Insert rows
    sqlite_exec(db, "INSERT INTO users (name, age) VALUES ('Alice', 30)")
    sqlite_exec(db, "INSERT INTO users (name, age) VALUES ('Bob', 25)")
    sqlite_exec(db, "INSERT INTO users (name, age) VALUES ('Charlie', 35)")

    // Query all rows
    let rows: Array = sqlite_query(db, "SELECT name, age FROM users ORDER BY age")
    var i: int = 0
    while i < rows.length() {
        let row: Array = rows[i]
        let name: String = row[0]
        let age: String = row[1]
        print(name + " is " + age + " years old")
        i = i + 1
    }

    // Query with a condition
    let seniors: Array = sqlite_query(db, "SELECT name FROM users WHERE age >= 30")
    print("users 30+: " + int_to_string(seniors.length()))

    sqlite_close(db)
    return 0
}
Outputstdout
Bob is 25 years old
Alice is 30 years old
Charlie is 35 years old
users 30+: 2

How it works

sqlite_open returns an opaque database handle (*int, NULL on failure). The special path ":memory:" creates a temporary in-memory database that is discarded when the handle is closed. For persistent storage, pass a file path like "data.db".

sqlite_query returns an array of rows, where each row is an array of string values (SQLite stores all values as text internally). Column order matches the SELECT clause. sqlite_exec is used for statements that don't return data (CREATE, INSERT, UPDATE, DELETE).