#async #rusqlite #sqlite

tokio-rusqlite

Asynchronous handle for rusqlite library

3 releases (breaking)

Uses new Rust 2021

0.3.0 Sep 16, 2022
0.2.0 Jul 13, 2022
0.1.0 Apr 25, 2022

#143 in Asynchronous

Download history 3/week @ 2022-06-09 32/week @ 2022-06-16 43/week @ 2022-06-23 24/week @ 2022-06-30 36/week @ 2022-07-07 67/week @ 2022-07-14 94/week @ 2022-07-21 229/week @ 2022-07-28 216/week @ 2022-08-04 160/week @ 2022-08-11 430/week @ 2022-08-18 127/week @ 2022-08-25 288/week @ 2022-09-01 4480/week @ 2022-09-08 4631/week @ 2022-09-15 4744/week @ 2022-09-22

14,179 downloads per month

MIT license

12KB
113 lines

License Crates.io Docs - Stable

tokio-rusqlite

Asynchronous handle for rusqlite library.

Usage

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

#[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::<Result<Vec<Person>, rusqlite::Error>>()?;

            Ok::<_, rusqlite::Error>(people)
        })
        .await?;

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

    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

~22MB
~409K SLoC