#sqlite #tokio #async #async-context #rusqlite

async-sqlite

A library for working with sqlite asynchronously

7 unstable releases (3 breaking)

0.3.1 Jul 23, 2024
0.3.0 Jul 3, 2024
0.2.2 Feb 9, 2024
0.2.1 Dec 29, 2023
0.0.1 Jun 25, 2023

#610 in Database interfaces

Download history 500/week @ 2024-07-29 225/week @ 2024-08-05 378/week @ 2024-08-12 442/week @ 2024-08-19 362/week @ 2024-08-26 456/week @ 2024-09-02 485/week @ 2024-09-09 487/week @ 2024-09-16 290/week @ 2024-09-23 236/week @ 2024-09-30 309/week @ 2024-10-07 551/week @ 2024-10-14 439/week @ 2024-10-21 366/week @ 2024-10-28 508/week @ 2024-11-04 361/week @ 2024-11-11

1,685 downloads per month
Used in utiles

MIT license

27KB
405 lines

async-sqlite

A library to interact with sqlite from an async context.

This library is tested on both tokio and async_std, however it should be compatible with all async runtimes.

Install

Add async-sqlite to your "dependencies" in your Cargo.toml file.

This can be done by running the command:

cargo add async-sqlite

Usage

A Client represents a single background sqlite3 connection that can be called concurrently from any thread in your program.

To create a sqlite client and run a query:

use async_sqlite::{ClientBuilder, JournalMode};

let client = ClientBuilder::new()
                .path("/path/to/db.sqlite3")
                .journal_mode(JournalMode::Wal)
                .open()
                .await?;

let value: String = client.conn(|conn| {
    conn.query_row("SELECT val FROM testing WHERE id=?", [1], |row| row.get(0))
}).await?;

println!("Value is: {value}");

A Pool represents a collection of background sqlite3 connections that can be called concurrently from any thread in your program.

To create a sqlite pool and run a query:

use async_sqlite::{JournalMode, PoolBuilder};

let pool = PoolBuilder::new()
              .path("/path/to/db.sqlite3")
              .journal_mode(JournalMode::Wal)
              .open()
              .await?;

let value: String = pool.conn(|conn| {
    conn.query_row("SELECT val FROM testing WHERE id=?", [1], |row| row.get(0))
}).await?;

println!("Value is: {value}");

Cargo Features

This library tries to export almost all features that the underlying rusqlite library contains.

A notable difference is that the bundled feature is enabled by default, but can be disabled with the following line in your Cargo.toml:

async-sqlite = { version = "*", default-features = false }

Dependencies

~29MB
~470K SLoC