expression
rusqlite: create a table and insert a row
Use execute for a single statement that changes data or schema and does not return rows. For an INSERT, the returned count is the number of rows changed by that statement. Use synthetic data and handle database errors with Result. Run in the accompanying Cargo project. Expected stdout (not executed): inserted: 1 Common mistake: Using execute for a SELECT that returns rows.
Formal Syntax & Structure
rust syntaxrust
conn.execute(sql, params)?Working Example
Example Snippetrust
fn main() -> Result<(), Box<dyn std::error::Error>> { let db = rusqlite::Connection::open_in_memory()?; db.execute("CREATE TABLE item (id INTEGER PRIMARY KEY, name TEXT NOT NULL)", [])?; let changed = db.execute("INSERT INTO item (name) VALUES (?1)", ["Pen"])?; println!("inserted: {changed}"); Ok(())}