Crate asqlite

Source
Expand description

§asqlite - Async SQLite for Rust

This library provides an API for accessing SQLite databases using async Rust.

It wraps libsqlite3, dispatching operations in a background thread.

§Usage

Add to your Cargo.toml:

[dependencies]
asqlite = { version = "1.2.0", features = [ "bundled" ] }

Unless you are writing a library, you probably want to enable the bundled feature, which automatically compiles SQLite. See Cargo features for more.

Start by creating a Connection via Connection::builder.

§Example

use futures::StreamExt;
use std::io;

#[tokio::main]
async fn main() -> io::Result<()> {
    // Create an in-memory database connection with the name :memory
    let mut conn = asqlite::Connection::builder()
        .create(true) // create if it does not exists
        .write(true) // read and write
        .open_memory(":memory") // to open a file, use .open(path)
        .await?;

    // Create a table
    conn.execute(
        "CREATE TABLE fruit (name TEXT, color TEXT)",
        (), // no parameters
    )
    .await?;

    // Populate the table
    let _apple_id = conn
        .insert(
            "INSERT INTO fruit (name, color) VALUES (?, ?)",
            asqlite::params!("apple", "red"),
        )
        .await?;

    // Check all red fruits
    let mut rows = conn.query(
        "SELECT name FROM fruit WHERE color = ?",
        asqlite::params!("red"),
    );

    // Iterate rows
    while let Some(row) = rows.next().await {
        let name: String = row?;

        println!("{} is red", name);
    }

    Ok(())
}

§Cancel safety

All operations of this library are cancel-safe and can be used with, for example, tokio::select!.

§Cargo features

  • bundled (disabled by default): automatically compiles and statically links an up to date version of SQLite to the library. This is a very good choice for most applications.

Modules§

convert
Types and traits for converting from and to SQLite data types.

Macros§

params
Creates a parameter list.

Structs§

Blob
Binary blob.
Connection
A SQLite3 connection.
ConnectionBuilder
Options for building a Connection.
Error
Library error.
InterruptHandle
Interrupt handle for a connection.
Statement
Prepared statement.
ZeroBlob
A zero filled binary blob.

Enums§

BlobOpenMode
Open mode for blobs.
ErrorKind
Library error kind.

Type Aliases§

Result
Alias for Result<T, Error>.