How to Connect to SQLite from Rust

Connect to SQLite in Rust by adding the diesel crate with sqlite and bundled features to Cargo.toml and initializing a SqliteConnection.

Add the diesel crate with the sqlite and libsqlite3-sys/bundled features to your Cargo.toml to compile SQLite directly into your binary.

[dependencies]
diesel = { version = "2.2", features = ["sqlite", "libsqlite3-sys/bundled"] }

In your Rust code, import the connection type and establish a connection to your database file:

use diesel::prelude::*;
use diesel::SqliteConnection;

fn main() {
    let mut connection = SqliteConnection::establish("file:my_database.sqlite?mode=ro").unwrap();
    println!("Connected to SQLite");
}