Home / Learn Secure SQL / Rust: sqlx Compile-Time Checked Queries

Rust: sqlx Compile-Time Checked Queries

Placeholders checked against the real schema at compile time.

In shortPlaceholders checked against the real schema at compile time.

What's actually going on

sqlx patternsqlx::query!("...WHERE email = $1",…checked against theschema at compile…rows returned,strongly typed
sqlx pattern

The basic idea

Rust's sqlx crate supports bound parameters with $1-style (or ? for SQLite/MySQL) placeholders, and can optionally check your SQL against the real database schema when you compile your code.

Why this comes up

Compile-time checking is an extra safety net on top of parameter binding: a typo in a column name, or a type mismatch in a bound parameter, becomes a build error instead of a runtime surprise.

The mechanics

Use the query! macro (or the plain query() function) with bound parameters; let sqlx's compile-time checking catch schema mismatches early if you enable it.

Where it is used

  • Web application code review
  • Backend development
  • Database administration
Rust sqlx
let row = sqlx::query!(
    "SELECT id, name FROM users WHERE email = $1",
    email
)
.fetch_one(&pool)
.await?;

Quick check

1. A notable extra feature of sqlx is...