Skip to main content

Crate sqlmodel_sqlite

Crate sqlmodel_sqlite 

Source
Expand description

SQLite driver for SQLModel Rust.

sqlmodel-sqlite is the SQLite driver for the SQLModel ecosystem. It implements the Connection trait from sqlmodel-core, providing a lightweight backend that is ideal for local development, embedded use, and testing.

§Role In The Architecture

  • Implements sqlmodel-core::Connection for SQLite
  • Supplies FFI-backed execution and type conversion
  • Enables sqlmodel-query and sqlmodel-session to run against SQLite

This crate provides a SQLite database driver using FFI bindings to libsqlite3. It implements the Connection trait from sqlmodel-core for seamless integration with the rest of the SQLModel ecosystem.

§Features

  • Full Connection trait implementation
  • Transaction support with savepoints
  • Type-safe parameter binding
  • In-memory and file-based databases
  • Configurable open flags and busy timeout

§Example

use sqlmodel_sqlite::{SqliteConnection, SqliteConfig};
use sqlmodel_core::{Connection, Value, Cx, Outcome};

// Open an in-memory database
let conn = SqliteConnection::open_memory().unwrap();

// Create a table
conn.execute_raw("CREATE TABLE users (id INTEGER PRIMARY KEY, name TEXT)").unwrap();

// Insert data using the Connection trait
let cx = Cx::for_testing();
match conn.insert(&cx, "INSERT INTO users (name) VALUES (?)", &[Value::Text("Alice".into())]).await {
    Outcome::Ok(id) => println!("Inserted user with id: {}", id),
    Outcome::Err(e) => eprintln!("Error: {}", e),
    _ => {}
}

§Type Mapping

Rust TypeSQLite Type
boolINTEGER (0/1)
i8, i16, i32INTEGER
i64INTEGER
f32, f64REAL
StringTEXT
Vec<u8>BLOB
Option<T>NULL or T
Date, Time, TimestampTEXT (ISO-8601)
UuidBLOB (16 bytes)
JsonTEXT

§Thread Safety

SqliteConnection is both Send and Sync, using internal mutex synchronization to protect the underlying SQLite handle. This allows connections to be shared across async tasks safely.

Re-exports§

pub use connection::OpenFlags;
pub use connection::SqliteConfig;
pub use connection::SqliteConnection;
pub use connection::SqliteTransaction;

Modules§

connection
SQLite connection implementation.
ffi
Low-level FFI bindings to libsqlite3.
types
Type encoding and decoding between Rust and SQLite.

Functions§

sqlite_version
Re-export the SQLite library version.
sqlite_version_number
Re-export the SQLite library version number.