sqlite3

moon add myfreess/sqlite3@0.1.1
Download zip
Author
Version
0.1.1
License
Apache-2.0
Last updated
9 months ago
Downloads
1K
README

#MoonBit SQLite3 Binding

A MoonBit binding library for SQLite3 database, providing a safe and ergonomic interface to SQLite3 database operations.

Note: currently README.md are generated by LLM, waiting for checking

#Installation

Add the dependency to your moon.mod.json:

moon add myfreess/sqlite3

Then import it in your package's moon.pkg.json:

{ "import": [ "myfreess/sqlite3" ] }

#Quick Start

fn main {
// Open database connection
let conn = @sqlite3.Connection::open("example.db")

// Create table
let stmt = conn.prepare("CREATE TABLE users (id INTEGER PRIMARY KEY, name TEXT, age INTEGER)")
stmt.step_once()
stmt.finalize()

// Insert data
let stmt = conn.prepare("INSERT INTO users (name, age) VALUES (?, ?)")
stmt.bind_string_as_blob(index=1, val="Alice")
stmt.bind(index=2, val=25)
stmt.step_once()
stmt.finalize()

// Query data
let stmt = conn.prepare("SELECT id, name, age FROM users WHERE age > ?")
stmt.bind(index=1, val=20)

while stmt.step() {
let id : Int = stmt.column(index=0)
let name : String = stmt.column_blob_as_string(index=1)
let age : Int = stmt.column(index=2)
println("User: id=\{id}, name=\{name}, age=\{age}")
}

stmt.finalize()
conn.close()
}

#API Reference

#Connection

#Connection::open(filename: String) -> Connection

Opens a database connection to the specified file.

let conn = @sqlite3.Connection::open("database.db")
// For in-memory database:
let conn = @sqlite3.Connection::open(":memory:")

#Connection::close(self) -> Unit

Closes the database connection.

#Connection::prepare(self, sql: String) -> Statement

Prepares an SQL statement for execution.

#Connection::get_errmsg(self) -> String

Returns the last error message for this connection.

#Statement

#Statement::bind(self, index: Int, val: T) -> Unit

Binds a parameter to the prepared statement. Supports the following types:
  • Int
  • Int64
  • Double
  • Bytes

stmt.bind(index=1, val=42) // Bind integer
stmt.bind(index=2, val=3.14) // Bind double
stmt.bind(index=3, val=some_bytes) // Bind bytes

#Statement::bind_string_as_blob(self, index: Int, val: String) -> Unit

Binds a string as a BLOB parameter.

stmt.bind_string_as_blob(index=1, val="Hello, World!")

#Statement::step(self) -> Bool

Executes the statement and returns true if a row is available, false if done.

#Statement::step_once(self) -> Unit

Executes the statement once. Throws an error if a row is returned.

#Statement::column(self, index: Int) -> T

Retrieves a column value. Supports the following types:
  • Int
  • Int64
  • Double
  • Bytes

let id : Int = stmt.column(index=0)
let price : Double = stmt.column(index=1)
let data : Bytes = stmt.column(index=2)

#Statement::column_blob_as_string(self, index: Int) -> String

Retrieves a BLOB column as a string.

#Statement::finalize(self) -> Unit

Finalizes the statement and releases resources.

#Error Handling

The library uses MoonBit's exception system with SqliteError:

fn example() -> Unit raise SqliteError {
let conn = @sqlite3.Connection::open("test.db")
// Database operations that might throw SqliteError
conn.close()
}

// Handle errors with try-catch
try {
example()
} catch {
SqliteError(code, loc) => println("SQLite error \{code} at \{loc}")
}

#Type Mapping

MoonBit TypeSQLite TypeNotes
IntINTEGER32-bit signed integer
Int64INTEGER64-bit signed integer
DoubleREALDouble-precision floating point
BytesBLOBBinary data
StringBLOBUse bind_string_as_blob/column_blob_as_string

#Complete Example

fn database_example() -> Unit raise SqliteError {
// Open database
let conn = @sqlite3.Connection::open(":memory:")

// Create table
let create_stmt = conn.prepare(
"CREATE TABLE products (id INTEGER PRIMARY KEY, name TEXT, price REAL, data BLOB)"
)
create_stmt.step_once()
create_stmt.finalize()

// Insert data
let insert_stmt = conn.prepare(
"INSERT INTO products (name, price, data) VALUES (?, ?, ?)"
)
insert_stmt.bind_string_as_blob(index=1, val="Widget")
insert_stmt.bind(index=2, val=19.99)
insert_stmt.bind(index=3, val=b"\x01\x02\x03")
insert_stmt.step_once()
insert_stmt.finalize()

// Query data
let select_stmt = conn.prepare("SELECT * FROM products WHERE price > ?")
select_stmt.bind(index=1, val=10.0)

while select_stmt.step() {
let id : Int = select_stmt.column(index=0)
let name = select_stmt.column_blob_as_string(index=1)
let price : Double = select_stmt.column(index=2)
let data : Bytes = select_stmt.column(index=3)

println("Product: \{id}, \{name}, $\{price}")
}

select_stmt.finalize()
conn.close()
}

#SQLite Constants

The library exposes SQLite result codes as constants:

  • SQLITE_OK (0) - Success
  • SQLITE_ERROR (1) - SQL error or missing database
  • SQLITE_ROW (100) - Step has returned a row
  • SQLITE_DONE (101) - Step has finished executing
  • And many more...

#Platform Support

Currently supports native target only. WebAssembly support may be added in future versions.

#Contributing

This library is a binding to the SQLite3 C library. Contributions are welcome for:

  • Additional utility functions
  • Better error messages
  • Documentation improvements
  • Test coverage

#License

This project is licensed under the Apache-2.0 License - see the LICENSE file for details.

#
Bind

trait Bind

impl Bind for Int
impl Bind for Int64
impl Bind for Double
impl Bind for Bytes

#
Column

trait Column

impl Column for Int
impl Column for Int64
impl Column for Double
impl Column for Bytes

#
SqliteError

pub suberror SqliteError (Int, SourceLoc)

impl Show for SqliteError

#
Connection

#external
pub type Connection

#
Connection::close

#callsite(autofill(loc))
fn Connection::close(self : Connection, loc~ : SourceLoc) -> Unit raise SqliteError

#
Connection::get_errmsg

fn Connection::get_errmsg(conn : Connection) -> String

#
Connection::open

#callsite(autofill(loc))
fn Connection::open(filename : String, loc~ : SourceLoc) -> Connection raise SqliteError

#
Connection::prepare

#callsite(autofill(loc))
fn Connection::prepare(self : Connection, stmt : String, loc~ : SourceLoc) -> Statement raise SqliteError

#
Statement

#external
pub type Statement

#
Statement::bind

#callsite(autofill(loc))
fn[T : Bind] Statement::bind(self : Statement, index~ : Int, val~ : T, loc~ : SourceLoc) -> Unit raise SqliteError

#
Statement::bind_string_as_blob

#callsite(autofill(loc))
fn Statement::bind_string_as_blob(self : Statement, index~ : Int, val~ : String, loc~ : SourceLoc) -> Unit raise SqliteError

#
Statement::column

fn[T : Column] Statement::column(self : Statement, index~ : Int) -> T

#
Statement::column_blob_as_string

fn Statement::column_blob_as_string(self : Statement, index~ : Int) -> String

#
Statement::finalize

#callsite(autofill(loc))
fn Statement::finalize(self : Statement, loc~ : SourceLoc) -> Unit raise SqliteError

#
Statement::step

#callsite(autofill(loc))
fn Statement::step(self : Statement, loc~ : SourceLoc) -> Bool raise SqliteError

#
Statement::step_once

#callsite(autofill(loc))
fn Statement::step_once(self : Statement, loc~ : SourceLoc) -> Unit raise SqliteError

#
SQLITE_ABORT

let SQLITE_ABORT : Int

#
SQLITE_AUTH

let SQLITE_AUTH : Int

#
SQLITE_BLOB

let SQLITE_BLOB : Int

#
SQLITE_BUSY

let SQLITE_BUSY : Int

#
SQLITE_CANTOPEN

let SQLITE_CANTOPEN : Int

#
SQLITE_CONSTRAINT

let SQLITE_CONSTRAINT : Int

#
SQLITE_CORRUPT

let SQLITE_CORRUPT : Int

#
SQLITE_DONE

let SQLITE_DONE : Int

#
SQLITE_EMPTY

let SQLITE_EMPTY : Int

#
SQLITE_ERROR

let SQLITE_ERROR : Int

#
SQLITE_ERROR_MISSING_COLLSEQ

let SQLITE_ERROR_MISSING_COLLSEQ : Int

#
SQLITE_FLOAT

let SQLITE_FLOAT : Int

#
SQLITE_FORMAT

let SQLITE_FORMAT : Int

#
SQLITE_FULL

let SQLITE_FULL : Int

#
SQLITE_INTEGER

let SQLITE_INTEGER : Int

#
SQLITE_INTERNAL

let SQLITE_INTERNAL : Int

#
SQLITE_INTERRUPT

let SQLITE_INTERRUPT : Int

#
SQLITE_IOERR

let SQLITE_IOERR : Int

#
SQLITE_LOCKED

let SQLITE_LOCKED : Int

#
SQLITE_MISMATCH

let SQLITE_MISMATCH : Int

#
SQLITE_MISUSE

let SQLITE_MISUSE : Int

#
SQLITE_NOLFS

let SQLITE_NOLFS : Int

#
SQLITE_NOMEM

let SQLITE_NOMEM : Int

#
SQLITE_NOTADB

let SQLITE_NOTADB : Int

#
SQLITE_NOTFOUND

let SQLITE_NOTFOUND : Int

#
SQLITE_NOTICE

let SQLITE_NOTICE : Int

#
SQLITE_OK

let SQLITE_OK : Int

#
SQLITE_OK_LOAD_PERMANENTLY

let SQLITE_OK_LOAD_PERMANENTLY : Int

#
SQLITE_PERM

let SQLITE_PERM : Int

#
SQLITE_PROTOCOL

let SQLITE_PROTOCOL : Int

#
SQLITE_RANGE

let SQLITE_RANGE : Int

#
SQLITE_READONLY

let SQLITE_READONLY : Int

#
SQLITE_ROW

let SQLITE_ROW : Int

#
SQLITE_SCHEMA

let SQLITE_SCHEMA : Int

#
SQLITE_TOOBIG

let SQLITE_TOOBIG : Int

#
SQLITE_WARNING

let SQLITE_WARNING : Int

Powered by MoonBit

Site sourceReport issuePackagesBuild queueSkillsStatistics

© 2026 mooncakes.io