rusqlite error when using parameters in query. "InvalidParameterName"

Could anyone help me understand why this code doesn't work?

It panics with InvalidParameterName(":floor_number")

I'm using rusqlite v0.31.0 with "bundled" feature.

pub fn park_vehicle(
    &self,
    vehicle_entry_time: i64,
    floor_number: i32,
    spot_number: i32,
) -> Result<(), Error> {
    let mut stmt = self.connection.prepare(
        "
        INSERT INTO vehicle(entry_time) VALUES (:entry_time);
        UPDATE parking_spot SET parked_vehicle_id = last_insert_rowid() WHERE floor_number = :floor_number AND spot_number = :spot_number;",
    )?;

    stmt.execute(named_params! {
        ":entry_time": vehicle_entry_time,
        ":floor_number": floor_number,
        ":spot_number": spot_number,
    })?;

    Ok(())
}

Upvotes: 0

Views: 106

Answers (1)

kmdreko
kmdreko

Reputation: 60187

prepare() only prepares a single statement while you're trying to use it with two. So it is only processing/preparing the first one where :floor_number is not used and thus you get the error. There is an open issue to return a warning earlier on prepare(), but that appears not yet implemented.

From this other issue you'll need to prepare the statements individually and execute them in sequence - potentially within a transation() if that is required.

Upvotes: 0

Related Questions