sqlite-wasm-rs 0.1.1

Wrap sqlite-wasm, and expect to provide a usable C-like API.
Documentation
sqlite-wasm-rs-0.1.1 has been yanked.

SQLite Wasm Rust

Wrap the official sqlite-wasm, and expect to provide a usable C-like API.

And currently the following APIs are implemented and tested:

1 2 3 4 5 6
sqlite3_open_v2 sqlite3_exec sqlite3_close sqlite3_close_v2 sqlite3_changes sqlite3_deserialize
sqlite3_serialize sqlite3_free sqlite3_create_function_v2 sqlite3_result_text sqlite3_result_blob sqlite3_result_int
sqlite3_result_int64 sqlite3_result_double sqlite3_result_null sqlite3_column_value sqlite3_column_count sqlite3_column_name
sqlite3_bind_null sqlite3_bind_blob sqlite3_bind_text sqlite3_value_free sqlite3_value_bytes sqlite3_value_text
sqlite3_value_blob sqlite3_value_int sqlite3_value_int64 sqlite3_value_double sqlite3_value_type sqlite3_value_dup
sqlite3_bind_double sqlite3_bind_int sqlite3_bind_int64 sqlite3_create_collation_v2 sqlite3_extended_errcode sqlite3_finalize
sqlite3_step sqlite3_errmsg sqlite3_db_handle sqlite3_reset sqlite3_prepare_v3 sqlite3_context_db_handle
sqlite3_user_data sqlite3_aggregate_context sqlite3_result_error

Usage

use sqlite_wasm_rs::c as ffi;
use std::ffi::CString;

async fn open_db() -> anyhow::Result<()> {
    // Before using CAPI, you must initialize sqlite.
    //
    // Initializing the database is a one-time operation during
    // the life of the program.
    ffi::init_sqlite().await?;

    let mut db = std::ptr::null_mut();
    let filename = CString::new("mydb").unwrap();
    // Persistent Storage is supported, use opfs vfs.
    // This support is only available when sqlite is loaded from a
    // Worker thread, whether it's loaded in its own dedicated worker
    // or in a worker together with client code.
    //
    // See <https://sqlite.org/wasm/doc/trunk/persistence.md#opfs>
    let vfs = CString::new("opfs").unwrap();
    let ret = unsafe {
        ffi::sqlite3_open_v2(
            filename.as_ptr(),
            &mut db as *mut _,
            ffi::SQLITE_OPEN_READWRITE | ffi::SQLITE_OPEN_CREATE,
            // Using std::ptr::null() is a memory DB
            vfs.as_ptr(),
        )
    };
    assert_eq!(ffi::SQLITE_OK, ret);
}

About TEST

This project was successfully used in diesel, and diesel's integration tests and unit tests all run successfully (except for a few tests that required std::fs::* ), see sqlitest.gif.

Related Project

  • sqlite-wasm: SQLite Wasm conveniently wrapped as an ES Module.
  • sqlite-web-rs: A SQLite WebAssembly backend for Diesel.
  • rusqlite: Ergonomic bindings to SQLite for Rust.