expression
Diesel: open an in-memory SQLite connection
Diesel exposes a synchronous connection API for SQLite. A memory database lives with its connection and is suitable for tiny isolated examples. Opening it does not create application tables. Establishing and using a synchronous connection may block an async executor thread. Uses a disposable in-memory SQLite database. Expected stdout (not executed): value: 7 Common mistake: Sharing the same connection assumption across separate :memory: connections, or running synchronous database calls directly on an async worker.
Formal Syntax & Structure
rust syntaxrust
SqliteConnection::establish(":memory:")?Working Example
Example Snippetrust
use diesel::prelude::*;use diesel::sqlite::SqliteConnection;
fn main() -> Result<(), Box<dyn std::error::Error>> { let mut conn = SqliteConnection::establish(":memory:")?; let n: i32 = diesel::select(diesel::dsl::sql::<diesel::sql_types::Integer>("7")).get_result(&mut conn)?; println!("value: {n}"); Ok(())}