Logo
Articles Compilers Libraries Books MiniBooklets Assembly C++ Rust Linux CPU Others Videos
Advertisement

Article by Ayman Alheraki on February 16 2026 08:08 PM

Connecting to SQLite in Rust is ridiculously easy (and you get “real database” power)

Connecting to SQLite in Rust is ridiculously easy (and you get “real database” power)

SQLite is not a separate server you install and manage—it's an in-process, zero-configuration, transactional SQL engine that lives inside your application. That’s why it’s perfect for desktop apps, CLI tools, embedded utilities, caches, local storage, and even many backend workloads.

In Rust, you typically pick one of two excellent paths:

  • rusqlite: straightforward, synchronous, ergonomic wrapper—great for CLI/desktop/tools.

  • sqlx: async-first toolkit with compile-time checked SQL (when using its macros), supports SQLite too—great for async services.

Below is an article-style guide with extensive, copy/paste-ready examples.

Option A: rusqlite (sync) — fastest path to “it works”

1) Add dependency

Cargo.toml

Why bundled? On many setups (especially Windows), this makes life easier by building/using a bundled SQLite instead of relying on a system install—ideal for shipping a single binary. (rusqlite documents many feature flags and common usage via docs.rs.)

2) Create a DB, a table, and insert data

What you just got:

  • a persistent database file (app.db)

  • schema creation

  • parameter binding (safe against SQL injection for values)

  • a real transactional DB engine under the hood


3) Query rows into Rust structs


4) Updates, deletes, and “rows affected”


5) Transactions (the “professional” part that stays simple)

If you have multiple operations that must succeed together:


6) Returning IDs (last_insert_rowid)

Option B: sqlx (async) — when you want async + compile-time checked SQL

sqlx is an async SQL toolkit that supports SQLite and is known for its compile-time checked queries (using macros) without forcing an ORM/DSL.

1) Add dependencies

Cargo.toml

2) Async connect + schema + CRUD (runtime queries)

4) Transactions (async)

Which one should you choose?

Choose rusqlite when:

  • You’re writing a CLI tool, desktop app, automation utility, or anything mostly single-threaded/synchronous.

  • You want the simplest mental model and minimal dependencies.

Choose sqlx when:

  • You already run an async runtime (Tokio/Actix/etc.).

  • You want async pooling and the option of compile-time SQL checks.

  • Your project may later switch from SQLite to Postgres/MySQL with fewer conceptual changes.

Practical tips that save real time

  • Use parameters for values (?1, ?2, .bind(...)) to avoid SQL injection for data values.

  • Keep schema creation in execute_batch (rusqlite) or a startup migration step (sqlx).

  • SQLite concurrency reality: it’s amazing, but it’s still a single-file DB; design your write patterns thoughtfully (transactions, batching). SQLite is built as an in-process engine and is meant to be embedded.

Advertisements

Responsive Counter
General Counter
1166463
Daily Counter
859