9个稳定版本
1.4.3 | 2023年6月21日 |
---|---|
1.4.2 | 2023年6月16日 |
1.3.2 | 2022年5月22日 |
1.3.1 | 2022年4月20日 |
0.1.1 |
|
#2 in #sqlite3
每月479次下载
9MB
166K SLoC
专为lod包管理器和Unix系统构建的非常微型的sqlite包装器包。如果您需要完整的sqlite数据库包,请考虑使用rusqlite。
将库添加到项目中
在您的Cargo.toml中
[dependencies]
min-sqlite3-sys = "1.4"
在您的二进制crate的build.rs中
use std::{env, path::Path};
fn main() {
let home_path = env::var("HOME").expect("HOME environment variable is not set.");
let target_dir = Path::new(&home_path).join(".local/share/min_sqlite3_sys");
println!("cargo:rustc-link-arg=-Wl,-rpath={}", target_dir.display());
}
用法
简单用法
use std::path::Path;
use min_sqlite3_sys::prelude::*;
fn main() {
let db = Database::open(Path::new("example.db")).unwrap();
let statement = String::from(
"CREATE TABLE IF NOT EXISTS items(
id PRIMARY KEY,
name TEXT,
tag TEXT
);
",
);
let status = db.execute(
statement,
None::<Box<dyn FnOnce(SqlitePrimaryResult, String)>>,
).unwrap();
if status != SqlitePrimaryResult::Ok {
// handle the problem
}
db.close();
}
带回调函数的简单用法
use std::path::Path;
use min_sqlite3_sys::prelude::*;
fn callback_function(status: SqlitePrimaryResult, sql_statement: String) {
println!(
"{} did not successfully executed. The error status is: {:?}.",
sql_statement, status
);
}
fn main() {
let db = Database::open(Path::new("example.db"));
let statement = String::from(
"CREATE TABLE IF NOT EXISTS items(
id PRIMARY KEY,
name TEXT,
tag TEXT
);
",
);
db.execute(statement, Some(callback_function)).unwrap();
db.close();
}
带检索数据的简单用法
#![allow(dead_code)]
use std::path::Path;
use min_sqlite3_sys::prelude::*;
fn callback_function(status: SqlitePrimaryResult, sql_statement: String) {
println!(
"{} did not successfully executed. The error status is: {:?}.",
sql_statement, status
);
}
#[derive(Debug)]
struct Item {
id: i64,
name: String,
tag: String,
}
fn main() {
let db = Database::open(Path::new("example.db")).unwrap();
let statement = String::from("SELECT * FROM items WHERE name = 'Onur';");
let mut sql = db.prepare(statement, Some(callback_function)).unwrap();
// Iterate the results
while let PreparedStatementStatus::FoundRow = sql.execute_prepared() {
println!(
"id = {}, name = {}, tag = {}",
sql.get_data::<i64>(0).unwrap(),
sql.get_data::<String>(1).unwrap(),
sql.get_data::<String>(2).unwrap(),
);
// Or simply
println!(
"{:?}",
Item {
id: sql.get_data(0).unwrap(),
name: sql.get_data(1).unwrap(),
tag: sql.get_data(2).unwrap(),
}
);
}
// Must be called for each `prepare()` result.
sql.kill();
db.close();
}
带检索数据+绑定值的简单用法
#![allow(dead_code)]
use std::path::Path;
use min_sqlite3_sys::prelude::*;
fn callback_function(status: SqlitePrimaryResult, sql_statement: String) {
println!(
"{} did not successfully executed. The error status is: {:?}.",
sql_statement, status
);
}
#[derive(Debug)]
struct Item {
id: i64,
name: String,
tag: String,
}
fn main() {
let db = Database::open(Path::new("example.db")).unwrap();
let statement = String::from("SELECT * FROM items WHERE name = ?;");
let mut sql = db.prepare(statement, Some(callback_function)).unwrap();
let status = sql.bind_val(1, "Onur");
// You can do some checks by
assert_eq!(status, SqlitePrimaryResult::Ok);
// or
if status == SqlitePrimaryResult::Range {
panic!("Out of index on sql.bind_val!");
}
// Iterate the results
while let PreparedStatementStatus::FoundRow = sql.execute_prepared() {
println!(
"id = {}, name = {}, tag = {}",
sql.get_data::<i64>(0).unwrap(),
sql.get_data::<String>(1).unwrap(),
sql.get_data::<String>(2).unwrap(),
);
// Or simply
println!(
"{:?}",
Item {
id: sql.get_data(0).unwrap(),
name: sql.get_data(1).unwrap(),
tag: sql.get_data(2).unwrap(),
}
);
}
// Must be called for each `prepare()` result.
sql.kill();
db.close();
}
注意
为了不膨胀您项目的构建输出,该库通过FFI使用C ABI从动态库执行sqlite函数。这意味着您的构建输出将不包括sqlite源代码。
此库不使用系统中的任何SQLite库,以确保该包不受SQLite版本的影响。相反,sqlite3-builder crate将sqlite3源代码编译为动态库,并将其放在'~/.local/share/min_sqlite3_sys'下。
许可证
本包受MIT许可证保护。有关更多信息,请参阅LICENSE文件。