sqliterustrusqlite

rusqlite returns Err(InvalidParameterCount(1, 0))


I try to execute a simple sqlite statement with rusqlite but get an error everytime.

let seconds = 1;

conn.execute(
    "DELETE FROM session WHERE created < DATETIME('now', '?1 seconds')",
    params![seconds],
)?;

returns an Err(InvalidParameterCount(1, 0)).

If I use a static string instead of a parameter, the query works. E.g.

conn.execute(
    "DELETE FROM session WHERE created < DATETIME('now', '1 seconds')",
    params![seconds],
)?;

How do I get the parameter in there?


Solution

  • You are writing the ?1 inside a string, that is not a parameter, but a normal couple of characters. Thus when you specify the actual parameter, it has no match because the query does not have any parameter placeholder.

    You want something like this (untested):

    let seconds = 1;
    
    conn.execute(
        "DELETE FROM session WHERE created < DATETIME('now', ?1)",
        params![format!("{} seconds", seconds)],
    )?;