7 releases (breaking)

0.6.0 Sep 20, 2024
0.5.1 Feb 26, 2024
0.5.0 Nov 25, 2023
0.4.0 Apr 2, 2023
0.1.0 Apr 25, 2022

#118 in Database interfaces

Download history 1715/week @ 2024-07-21 2013/week @ 2024-07-28 1510/week @ 2024-08-04 1627/week @ 2024-08-11 2163/week @ 2024-08-18 2587/week @ 2024-08-25 2172/week @ 2024-09-01 2664/week @ 2024-09-08 2728/week @ 2024-09-15 2492/week @ 2024-09-22 3041/week @ 2024-09-29 2923/week @ 2024-10-06 2914/week @ 2024-10-13 2946/week @ 2024-10-20 3319/week @ 2024-10-27 3062/week @ 2024-11-03

12,349 downloads per month
Used in 13 crates (12 directly)

MIT license

25KB
416 lines

License Crates.io Docs - Stable

tokio-rusqlite

Asynchronous handle for rusqlite library.

Usage

use tokio_rusqlite::{params, Connection, Result};

#[derive(Debug)]
struct Person {
    id: i32,
    name: String,
    data: Option<Vec<u8>>,
}

#[tokio::main]
async fn main() -> Result<()> {
    let conn = Connection::open_in_memory().await?;

    let people = conn
        .call(|conn| {
            conn.execute(
                "CREATE TABLE person (
                    id    INTEGER PRIMARY KEY,
                    name  TEXT NOT NULL,
                    data  BLOB
                )",
                [],
            )?;

            let steven =
                Person {
                    id: 1,
                    name: "Steven".to_string(),
                    data: None,
                };

            conn.execute(
                "INSERT INTO person (name, data) VALUES (?1, ?2)",
                params![steven.name, steven.data],
            )?;

            let mut stmt = conn.prepare("SELECT id, name, data FROM person")?;
            let people = stmt
                .query_map([], |row| {
                    Ok(Person {
                        id: row.get(0)?,
                        name: row.get(1)?,
                        data: row.get(2)?,
                    })
                })?
                .collect::<std::result::Result<Vec<Person>, rusqlite::Error>>()?;

            Ok(people)
        })
        .await?;

    for person in people {
        println!("Found person {person:?}");
    }

    conn.close().await?;
    Ok(())
}

Safety

This crate uses #![forbid(unsafe_code)] to ensure everything is implemented in 100% safe Rust.

License

This project is licensed under the MIT license.

Dependencies

~30MB
~473K SLoC