4 stable releases

3.39.4 Jun 11, 2023
3.38.7 Apr 19, 2022
3.38.6 Mar 30, 2022
3.38.5 Mar 28, 2022

#363 in Database interfaces

Download history 2/week @ 2024-02-09 170/week @ 2024-02-16 11/week @ 2024-02-23 11/week @ 2024-03-01

194 downloads per month
Used in min-sqlite3-sys

MIT license

9MB
164K SLoC

C 164K SLoC // 0.3% comments Rust 79 SLoC

Very minimal sqlite wrapper package built specifically for lod package manager and Unix systems. If you need complete box of sqlite database, consider using rusqlite.

Adding lib to the project

In your Cargo.toml:

[dependencies]
min-sqlite3-sys = "1.3"

In build.rs of your binary crate:

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());
}

Usage

Simple usage:

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();
}

Simple usage with callback function:

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")).unwrap();
    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();
}

Simple usage with retrieving some data:

#![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();
}

Simple usage with retrieving some data + binding values:

#![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();
}

Notes

In order to not inflate the build outputs of your projects, the library executes sqlite functions from dynamic library using C ABI via FFI. Meaning, your build output will not include sqlite sources.

This library does not use any SQLite library on your system to ensure that the package doesn't get affected by SQLite versions. Instead, the sqlite3-builder crate compiles the sqlite3 sources under the c_source directory as dynamic library and puts that under the '~/.local/share/min_sqlite3_sys'.

License

This package is covered under the MIT license. See the LICENSE file for more info.

No runtime deps