How to Use sqlx for Compile-Time Checked SQL Queries

Use `sqlx::query!` or `sqlx::query_as!` macros to compile-time check SQL syntax and column types against your database schema.

How to Use sqlx for Compile-Time Checked SQL Queries

Use sqlx::query! or sqlx::query_as! macros to compile-time check SQL syntax and column types against your database schema.

use sqlx::{query, PgPool};

#[tokio::main]
async fn main() {
    let pool = PgPool::connect("postgres://localhost").await.unwrap();
    let name: String = sqlx::query_scalar("SELECT name FROM users WHERE id = 1")
        .fetch_one(&pool)
        .await
        .unwrap();
}

Run cargo sqlx prepare to validate queries against your actual database schema during development.