0PricingLogin
Learn Rust Coding · Lesson

Compile-Time Checked Queries

Type-safe SQL.

The killer feature

sqlx can verify your SQL at compile time against a real database schema. Typos, wrong column names, and type mismatches become build errors.

  • Uses the query! family of macros
  • Catches bugs before runtime

query! vs query

The query() function is checked at runtime. The query! macro is checked at compile time by connecting to the database during the build.

use sqlx::{Pool, Postgres};

async fn run(pool: &Pool<Postgres>) {
    let rec = sqlx::query!("SELECT id, name FROM users WHERE id = $1", 1i32)
        .fetch_one(pool)
        .await
        .unwrap();
}

All lessons in this course

  1. Connecting to a Database
  2. Compile-Time Checked Queries
  3. CRUD Operations
  4. Migrations
← Back to Learn Rust Coding