expression

rusqlite: bind values with positional parameters

Placeholders keep data separate from SQL grammar. Bind text and numeric values through params!, rather than interpolating them into the SQL string. Quotes inside text remain part of the stored value. Identifiers such as table names cannot be supplied through value placeholders. Run in the accompanying Cargo project. Expected stdout (not executed): quantity: 3 Common mistake: Building SQL by concatenating a name supplied by the user.

Formal Syntax & Structure

rust syntaxrust
params![value1, value2]

Working Example

Example Snippetrust
fn main() -> Result<(), Box<dyn std::error::Error>> {    use rusqlite::{Connection, params};    let db = Connection::open_in_memory()?;    db.execute("CREATE TABLE item (name TEXT, qty INTEGER)", [])?;    db.execute("INSERT INTO item VALUES (?1, ?2)", params!["O'Reilly", 3_i64])?;    let qty: i64 = db.query_row("SELECT qty FROM item WHERE name = ?1", ["O'Reilly"], |r| r.get(0))?;    println!("quantity: {qty}");    Ok(())}
Rust Reference