moon add myfreess/sqlite3{
"import": [
"myfreess/sqlite3"
]
}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()
}let conn = @sqlite3.Connection::open("database.db")
// For in-memory database:
let conn = @sqlite3.Connection::open(":memory:")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 bytesstmt.bind_string_as_blob(index=1, val="Hello, World!")let id : Int = stmt.column(index=0)
let price : Double = stmt.column(index=1)
let data : Bytes = stmt.column(index=2)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}")
}| MoonBit Type | SQLite Type | Notes |
|---|---|---|
| Int | INTEGER | 32-bit signed integer |
| Int64 | INTEGER | 64-bit signed integer |
| Double | REAL | Double-precision floating point |
| Bytes | BLOB | Binary data |
| String | BLOB | Use bind_string_as_blob/column_blob_as_string |
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()
}trait Bindfn bind(stmt : Statement, param_index : Int, value : Int, loc~ : SourceLoc) -> Unit raise SqliteErrorfn bind(stmt : Statement, param_index : Int, value : Int64, loc~ : SourceLoc) -> Unit raise SqliteErrorfn bind(stmt : Statement, param_index : Int, value : Double, loc~ : SourceLoc) -> Unit raise SqliteErrortrait Column#external
pub type Connection#callsite(autofill(loc))
fn Connection::close(self : Connection, loc~ : SourceLoc) -> Unit raise SqliteError#callsite(autofill(loc))
fn Connection::open(filename : String, loc~ : SourceLoc) -> Connection raise SqliteError#callsite(autofill(loc))
fn Connection::prepare(self : Connection, stmt : String, loc~ : SourceLoc) -> Statement raise SqliteError#external
pub type Statement#callsite(autofill(loc))
fn[T : Bind] Statement::bind(self : Statement, index~ : Int, val~ : T, loc~ : SourceLoc) -> Unit raise SqliteError#callsite(autofill(loc))
fn Statement::bind_string_as_blob(self : Statement, index~ : Int, val~ : String, loc~ : SourceLoc) -> Unit raise SqliteError#callsite(autofill(loc))
fn Statement::finalize(self : Statement, loc~ : SourceLoc) -> Unit raise SqliteError#callsite(autofill(loc))
fn Statement::step(self : Statement, loc~ : SourceLoc) -> Bool raise SqliteError#callsite(autofill(loc))
fn Statement::step_once(self : Statement, loc~ : SourceLoc) -> Unit raise SqliteError