Crate bsqlite

Source
Expand description

§Bassie SQLight crate

A simple and minimal Rust SQLite library with an ergonomic API

§Example

A example that inserts and reads rows from and too structs:

use bsqlite::{Connection, FromRow};

#[derive(FromRow)]
struct NewPerson {
    name: String,
    age: i64,
}

#[derive(Debug, FromRow)]
struct Person {
    id: i64,
    name: String,
    age: i64,
}

fn main() {
    // Connect and create table
    let db = Connection::open_memory().expect("Can't open database");
    db.execute(
        "CREATE TABLE IF NOT EXISTS persons (
            id INTEGER PRIMARY KEY AUTOINCREMENT,
            name TEXT NOT NULL,
            age INTEGER NOT NULL
        ) STRICT",
        (),
    );

    // Insert a rows
    let persons = [
        NewPerson {
            name: "Alice".to_string(),
            age: 30,
        },
        NewPerson {
            name: "Bob".to_string(),
            age: 40,
        },
    ];
    for person in persons {
        db.execute(
            format!(
                "INSERT INTO persons ({}) VALUES ({})",
                NewPerson::columns(),
                NewPerson::values()
            ),
            person,
        );
    }

    // Read rows back
    let persons = db.query::<Person>(format!("SELECT {} FROM persons", Person::columns()), ());
    for person in persons {
        println!("{:?}", person); // -> Person { id: 1, name: "Alice", age: 30 }
    }
}

See the examples for many more examples.

§Design goals

  • Connect and execute queries on a SQLite database
  • Have a generic Value enum type to represent SQLite values
  • Bind and read Value types to and from SQLite statements
  • Have FromRow and FromValue derive macros to convert between Rust types to SQLite Value’s
  • Work well and efficient with popular crates like uuid and chrono
  • Have helpful error messages on query errors

§Documentation

See the documentation for more information.

§License

Copyright © 2024-2025 Bastiaan van der Plaat

Licensed under the MIT license.

Macros§

execute_args
Execute a query with named arguments
query_args
Run a query with named arguments

Structs§

Connection
A SQLite connection
ConnectionError
A connection error
RawStatement
Raw SQLite statement without type information
Statement
A SQLite statement with type information
ValueError
A value error

Enums§

Value
A SQLite value

Traits§

Bind
A trait for binding values to a statement
FromRow
A trait for converting read values from a statement to a row

Derive Macros§

FromRow
FromRow derive for structs
FromValue
FromValue derive for enums