| Crates.io | unistore-sqlite |
| lib.rs | unistore-sqlite |
| version | 0.1.0 |
| created_at | 2026-01-20 01:42:37.420554+00 |
| updated_at | 2026-01-20 01:42:37.420554+00 |
| description | SQLite embedded database capability for UniStore |
| homepage | https://github.com/yangbo1317/unistore |
| repository | https://github.com/yangbo1317/unistore |
| max_upload_size | |
| id | 2055635 |
| size | 107,300 |
UniStore SQLite 嵌入式数据库能力,提供类型安全的数据库操作。
unistore-sqlite 提供:
[dependencies]
unistore-sqlite = "0.1"
use unistore_sqlite::EmbeddedDb;
fn main() -> Result<(), Box<dyn std::error::Error>> {
// 打开或创建数据库
let db = EmbeddedDb::open("my_app")?;
// 定义表结构
db.migrate(|m| {
m.version(1, "创建用户表", |s| {
s.create_table("users", |t| {
t.id()
.text_not_null("name")
.text("email")
.created_at();
})
});
})?;
// 插入数据
let id = db.insert("users")
.set("name", "Alice")
.set("email", "alice@example.com")
.execute()?;
// 查询数据
let users = db.select("users")
.filter("id = ?", id)
.fetch_all()?;
// 更新数据
db.update("users")
.set("name", "Alice Smith")
.filter("id = ?", id)
.execute()?;
// 删除数据
db.delete("users")
.filter("id = ?", id)
.execute()?;
Ok(())
}
db.with_transaction(|tx| {
tx.execute("INSERT INTO users (name) VALUES (?)", &["Bob"])?;
tx.execute("INSERT INTO logs (action) VALUES (?)", &["created user"])?;
Ok(())
})?;
// 自动提交,出错自动回滚
// 内存数据库(测试用)
let db = EmbeddedDb::memory()?;
// 性能优先
let config = SqliteConfig::performance();
// 持久性优先
let config = SqliteConfig::durable();
需要直接使用 rusqlite 时:
db.with_connection(|conn| {
// conn 是 &rusqlite::Connection
conn.execute("PRAGMA optimize", [])?;
Ok(())
})?;
MIT OR Apache-2.0
本 crate 基于以下优秀项目构建:
感谢 rusqlite 团队和 SQLite 团队的杰出工作!