| Crates.io | bsqlite |
| lib.rs | bsqlite |
| version | 0.1.2 |
| created_at | 2025-02-10 21:49:29.856615+00 |
| updated_at | 2025-02-13 20:14:52.245213+00 |
| description | A simple and minimal Rust SQLite library with an ergonomic API |
| homepage | https://github.com/bplaat/crates/tree/master/lib/bsqlite |
| repository | https://github.com/bplaat/crates |
| max_upload_size | |
| id | 1550712 |
| size | 52,047 |
A simple and minimal Rust SQLite library with an ergonomic API
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.
Value enum type to represent SQLite valuesValue types to and from SQLite statementsFromRow and FromValue derive macros to convert between Rust types to SQLite Value'suuid and chronoSee the documentation for more information.
Copyright © 2024-2025 Bastiaan van der Plaat
Licensed under the MIT license.