sqlite

SQLite bindings for Kit

Files

FileDescription
.editorconfigEditor formatting configuration
.gitignoreGit ignore rules for build artifacts and dependencies
.tool-versionsasdf tool versions (Zig, Kit)
LICENSEMIT license file
README.mdThis file
dev/chinook.kitDevelopment script for chinook
dev/northwind.kitDevelopment script for northwind
dev/repl.kitDevelopment script for repl
examples/basic.kitBasic usage example
examples/chinook.kitExample: chinook
examples/northwind.kitExample: northwind
examples/sqlite.kitExample: sqlite
kit.tomlPackage manifest with metadata and dependencies
src/sqlite.kitSQLite Bindings for Kit
tests/sqlite.test.kitTests for sqlite

Dependencies

No Kit package dependencies.

  • kit-query - Fluent SQL query builder for Kit

Installation

kit add gitlab.com/kit-lang/packages/kit-sqlite.git

Usage

import Kit.Sqlite as SQLite

main = fn(-env: Env) =>
  # Connect to an in-memory database
  db = SQLite.connect ":memory:"
  defer SQLite.close db

  # Create a table
  SQLite.execute db.handle "CREATE TABLE users (id INTEGER PRIMARY KEY, name TEXT, age INTEGER)"

  # Insert data
  SQLite.execute db.handle "INSERT INTO users (name, age) VALUES ('Alice', 30)"
  SQLite.execute db.handle "INSERT INTO users (name, age) VALUES ('Bob', 25)"
  SQLite.execute db.handle "INSERT INTO users (name, age) VALUES ('Charlie', 35)"

  # Query with the high-level API
  match db.query "SELECT id, name, age FROM users"
    | Success rows -> println "Got ${List.length rows} rows"
    | Error msg -> println "Query failed: ${msg}"

main

REPL Preloaders

Pre-built REPL helpers for interactive exploration with famous sample databases:

kit repl --preload dev/repl.kit       # Generic in-memory database (users, posts)
kit repl --preload dev/chinook.kit    # Chinook music store (artists, albums, tracks)
kit repl --preload dev/northwind.kit  # Northwind traders (products, orders, customers)

Each preloader creates an in-memory database with sample data and provides helper functions for common queries. Type the preloader's suggested commands to get started.

Development

Running Examples

Run examples with the interpreter:

kit run examples/basic.kit

Compile examples to a native binary:

kit build examples/basic.kit && ./basic

Running Tests

Run the test suite:

kit test

Run the test suite with coverage:

kit test --coverage

Running kit dev

Run the standard development workflow (format, check, test):

kit dev

This will:

  1. Format and check source files in src/
  2. Run tests in tests/ with coverage

Generating Documentation

Generate API documentation from doc comments:

kit doc

Note: Kit sources with doc comments (##) will generate HTML documents in docs/*.html

Cleaning Build Artifacts

Remove generated files, caches, and build artifacts:

kit task clean

Note: Defined in kit.toml.

Local Installation

To install this package locally for development:

kit install

This installs the package to ~/.kit/packages/@kit/sqlite/, making it available for import as Kit.Sqlite in other projects.

License

This package is released under the MIT License - see LICENSE for details.

SQLite is in the public domain.

Exported Functions & Types

sqlite-ok

SQLite operation completed successfully.

sqlite-row

SQLite query returned a row.

sqlite-done

SQLite query completed with no more rows.

sqlite-type-int

SQLite column type for integer values.

sqlite-type-float

SQLite column type for floating point values.

sqlite-type-text

SQLite column type for text strings.

sqlite-type-blob

SQLite column type for binary blob data.

sqlite-type-null

SQLite column type for NULL values.

SQLiteError

SQLite error type for typed error handling. Variants distinguish between different failure modes.

Variants

SQLiteOpenError {message}
SQLitePrepareError {message}
SQLiteQueryError {message}
SQLiteExecError {message}
SQLiteBindError {message}

connect

Open a SQLite database connection and return a connection object with query/execute methods. Returns a connection record with query/execute methods.

String -> {handle: Ptr, driver: String, query: String -> Result List SQLiteError, execute: String -> Result Int SQLiteError}

db = SQLite.connect "example.sqlite"
result = db.query "SELECT * FROM users"

query

Execute a query and return all rows as a list of records with column names as keys. Each row is a Record with column names as keys. NULL values are represented as None, non-null values as Some value. Text values matching ISO8601 format (YYYY-MM-DD or YYYY-MM-DD HH:MM:SS) are automatically converted to DateTime.

Ptr -> NonEmptyString -> Result List SQLiteError

query-maps

Execute a query and return all rows as a list of maps with column names as keys. Each row is a Map with column names as keys. NULL values are represented as None, non-null values as Some value.

Ptr -> NonEmptyString -> Result List SQLiteError

query-raw

Execute a query and return all rows as a list of vectors in column order. Each row is a vector [col0, col1, col2, ...] in column order. This is more efficient than maps when you know the column order.

Ptr -> NonEmptyString -> Result List SQLiteError

execute

Execute a SQL statement (INSERT, UPDATE, DELETE, CREATE, etc.) and return affected row count. Returns number of affected rows on success, or error.

Ptr -> NonEmptyString -> Result Int SQLiteError