Trait rusqlite::Params

source ·
pub trait Params: Sealed { }
Expand description

Trait used for sets of parameter passed into SQL statements/queries.

Note: Currently, this trait can only be implemented inside this crate. Additionally, it’s methods (which are doc(hidden)) should currently not be considered part of the stable API, although it’s possible they will stabilize in the future.

§Passing parameters to SQLite

Many functions in this library let you pass parameters to SQLite. Doing this lets you avoid any risk of SQL injection, and is simpler than escaping things manually. Aside from deprecated functions and a few helpers, this is indicated by the function taking a generic argument that implements Params (this trait).

§Positional parameters

For cases where you want to pass a list of parameters where the number of parameters is known at compile time, this can be done in one of the following ways:

  • For small lists of parameters up to 16 items, they may alternatively be passed as a tuple, as in thing.query((1, "foo")).

    This is somewhat inconvenient for a single item, since you need a weird-looking trailing comma: thing.query(("example",)). That case is perhaps more cleanly expressed as thing.query(["example"]).

  • Using the rusqlite::params! macro, e.g. thing.query(rusqlite::params![1, "foo", bar]). This is mostly useful for heterogeneous lists where the number of parameters greater than 16, or homogeneous lists of parameters where the number of parameters exceeds 32.

  • For small homogeneous lists of parameters, they can either be passed as:

    • an array, as in thing.query([1i32, 2, 3, 4]) or thing.query(["foo", "bar", "baz"]).

    • a reference to an array of references, as in thing.query(&["foo", "bar", "baz"]) or thing.query(&[&1i32, &2, &3]).

      (Note: in this case we don’t implement this for slices for coherence reasons, so it really is only for the “reference to array” types — hence why the number of parameters must be <= 32 or you need to reach for rusqlite::params!)

    Unfortunately, in the current design it’s not possible to allow this for references to arrays of non-references (e.g. &[1i32, 2, 3]). Code like this should instead either use params!, an array literal, a &[&dyn ToSql] or if none of those work, ParamsFromIter.

  • As a slice of ToSql trait object references, e.g. &[&dyn ToSql]. This is mostly useful for passing parameter lists around as arguments without having every function take a generic P: Params.

§Example (positional)

fn update_rows(conn: &Connection) -> Result<()> {
    let mut stmt = conn.prepare("INSERT INTO test (a, b) VALUES (?1, ?2)")?;

    // Using a tuple:
    stmt.execute((0, "foobar"))?;

    // Using `rusqlite::params!`:
    stmt.execute(params![1i32, "blah"])?;

    // array literal — non-references
    stmt.execute([2i32, 3i32])?;

    // array literal — references
    stmt.execute(["foo", "bar"])?;

    // Slice literal, references:
    stmt.execute(&[&2i32, &3i32])?;

    // Note: The types behind the references don't have to be `Sized`
    stmt.execute(&["foo", "bar"])?;

    // However, this doesn't work (see above):
    // stmt.execute(&[1i32, 2i32])?;
    Ok(())
}

§Named parameters

SQLite lets you name parameters using a number of conventions (“:foo”, “@foo”, “$foo”). You can pass named parameters in to SQLite using rusqlite in a few ways:

  • Using the rusqlite::named_params! macro, as in stmt.execute(named_params!{ ":name": "foo", ":age": 99 }). Similar to the params macro, this is most useful for heterogeneous lists of parameters, or lists where the number of parameters exceeds 32.

  • As a slice of &[(&str, &dyn ToSql)]. This is what essentially all of these boil down to in the end, conceptually at least. In theory you can pass this as stmt.

  • As array references, similar to the positional params. This looks like thing.query(&[(":foo", &1i32), (":bar", &2i32)]) or thing.query(&[(":foo", "abc"), (":bar", "def")]).

Note: Unbound named parameters will be left to the value they previously were bound with, falling back to NULL for parameters which have never been bound.

§Example (named)

fn insert(conn: &Connection) -> Result<()> {
    let mut stmt = conn.prepare("INSERT INTO test (key, value) VALUES (:key, :value)")?;
    // Using `rusqlite::params!`:
    stmt.execute(named_params! { ":key": "one", ":val": 2 })?;
    // Alternatively:
    stmt.execute(&[(":key", "three"), (":val", "four")])?;
    // Or:
    stmt.execute(&[(":key", &100), (":val", &200)])?;
    Ok(())
}

§No parameters

You can just use an empty tuple or the empty array literal to run a query that accepts no parameters.

§Example (no parameters)

The empty tuple:

fn delete_all_users(conn: &Connection) -> Result<()> {
    // You may also use `()`.
    conn.execute("DELETE FROM users", ())?;
    Ok(())
}

The empty array:

fn delete_all_users(conn: &Connection) -> Result<()> {
    // Just use an empty array (e.g. `[]`) for no params.
    conn.execute("DELETE FROM users", [])?;
    Ok(())
}

§Dynamic parameter list

If you have a number of parameters which is unknown at compile time (for example, building a dynamic query at runtime), you have two choices:

  • Use a &[&dyn ToSql]. This is often annoying to construct if you don’t already have this type on-hand.
  • Use the ParamsFromIter type. This essentially lets you wrap an iterator some T: ToSql with something that implements Params. The usage of this looks like rusqlite::params_from_iter(something).

A lot of the considerations here are similar either way, so you should see the ParamsFromIter documentation for more info / examples.

Implementations on Foreign Types§

source§

impl Params for &[&dyn ToSql]

source§

impl Params for &[(&str, &dyn ToSql)]

source§

impl Params for ()

source§

impl Params for [&(dyn ToSql + Send + Sync); 0]

source§

impl<A, B> Params for (A, B)
where A: ToSql, B: ToSql,

source§

impl<A, B, C> Params for (A, B, C)
where A: ToSql, B: ToSql, C: ToSql,

source§

impl<A, B, C, D> Params for (A, B, C, D)
where A: ToSql, B: ToSql, C: ToSql, D: ToSql,

source§

impl<A, B, C, D, E> Params for (A, B, C, D, E)
where A: ToSql, B: ToSql, C: ToSql, D: ToSql, E: ToSql,

source§

impl<A, B, C, D, E, F> Params for (A, B, C, D, E, F)
where A: ToSql, B: ToSql, C: ToSql, D: ToSql, E: ToSql, F: ToSql,

source§

impl<A, B, C, D, E, F, G> Params for (A, B, C, D, E, F, G)
where A: ToSql, B: ToSql, C: ToSql, D: ToSql, E: ToSql, F: ToSql, G: ToSql,

source§

impl<A, B, C, D, E, F, G, H> Params for (A, B, C, D, E, F, G, H)
where A: ToSql, B: ToSql, C: ToSql, D: ToSql, E: ToSql, F: ToSql, G: ToSql, H: ToSql,

source§

impl<A, B, C, D, E, F, G, H, I> Params for (A, B, C, D, E, F, G, H, I)
where A: ToSql, B: ToSql, C: ToSql, D: ToSql, E: ToSql, F: ToSql, G: ToSql, H: ToSql, I: ToSql,

source§

impl<A, B, C, D, E, F, G, H, I, J> Params for (A, B, C, D, E, F, G, H, I, J)
where A: ToSql, B: ToSql, C: ToSql, D: ToSql, E: ToSql, F: ToSql, G: ToSql, H: ToSql, I: ToSql, J: ToSql,

source§

impl<A, B, C, D, E, F, G, H, I, J, K> Params for (A, B, C, D, E, F, G, H, I, J, K)
where A: ToSql, B: ToSql, C: ToSql, D: ToSql, E: ToSql, F: ToSql, G: ToSql, H: ToSql, I: ToSql, J: ToSql, K: ToSql,

source§

impl<A, B, C, D, E, F, G, H, I, J, K, L> Params for (A, B, C, D, E, F, G, H, I, J, K, L)
where A: ToSql, B: ToSql, C: ToSql, D: ToSql, E: ToSql, F: ToSql, G: ToSql, H: ToSql, I: ToSql, J: ToSql, K: ToSql, L: ToSql,

source§

impl<A, B, C, D, E, F, G, H, I, J, K, L, M> Params for (A, B, C, D, E, F, G, H, I, J, K, L, M)
where A: ToSql, B: ToSql, C: ToSql, D: ToSql, E: ToSql, F: ToSql, G: ToSql, H: ToSql, I: ToSql, J: ToSql, K: ToSql, L: ToSql, M: ToSql,

source§

impl<A, B, C, D, E, F, G, H, I, J, K, L, M, N> Params for (A, B, C, D, E, F, G, H, I, J, K, L, M, N)
where A: ToSql, B: ToSql, C: ToSql, D: ToSql, E: ToSql, F: ToSql, G: ToSql, H: ToSql, I: ToSql, J: ToSql, K: ToSql, L: ToSql, M: ToSql, N: ToSql,

source§

impl<A, B, C, D, E, F, G, H, I, J, K, L, M, N, O> Params for (A, B, C, D, E, F, G, H, I, J, K, L, M, N, O)
where A: ToSql, B: ToSql, C: ToSql, D: ToSql, E: ToSql, F: ToSql, G: ToSql, H: ToSql, I: ToSql, J: ToSql, K: ToSql, L: ToSql, M: ToSql, N: ToSql, O: ToSql,

source§

impl<A, B, C, D, E, F, G, H, I, J, K, L, M, N, O, P> Params for (A, B, C, D, E, F, G, H, I, J, K, L, M, N, O, P)
where A: ToSql, B: ToSql, C: ToSql, D: ToSql, E: ToSql, F: ToSql, G: ToSql, H: ToSql, I: ToSql, J: ToSql, K: ToSql, L: ToSql, M: ToSql, N: ToSql, O: ToSql, P: ToSql,

source§

impl<T: ToSql + ?Sized> Params for &[(&str, &T); 1]

source§

impl<T: ToSql + ?Sized> Params for &[(&str, &T); 2]

source§

impl<T: ToSql + ?Sized> Params for &[(&str, &T); 3]

source§

impl<T: ToSql + ?Sized> Params for &[(&str, &T); 4]

source§

impl<T: ToSql + ?Sized> Params for &[(&str, &T); 5]

source§

impl<T: ToSql + ?Sized> Params for &[(&str, &T); 6]

source§

impl<T: ToSql + ?Sized> Params for &[(&str, &T); 7]

source§

impl<T: ToSql + ?Sized> Params for &[(&str, &T); 8]

source§

impl<T: ToSql + ?Sized> Params for &[(&str, &T); 9]

source§

impl<T: ToSql + ?Sized> Params for &[(&str, &T); 10]

source§

impl<T: ToSql + ?Sized> Params for &[(&str, &T); 11]

source§

impl<T: ToSql + ?Sized> Params for &[(&str, &T); 12]

source§

impl<T: ToSql + ?Sized> Params for &[(&str, &T); 13]

source§

impl<T: ToSql + ?Sized> Params for &[(&str, &T); 14]

source§

impl<T: ToSql + ?Sized> Params for &[(&str, &T); 15]

source§

impl<T: ToSql + ?Sized> Params for &[(&str, &T); 16]

source§

impl<T: ToSql + ?Sized> Params for &[(&str, &T); 17]

source§

impl<T: ToSql + ?Sized> Params for &[(&str, &T); 18]

source§

impl<T: ToSql + ?Sized> Params for &[(&str, &T); 19]

source§

impl<T: ToSql + ?Sized> Params for &[(&str, &T); 20]

source§

impl<T: ToSql + ?Sized> Params for &[(&str, &T); 21]

source§

impl<T: ToSql + ?Sized> Params for &[(&str, &T); 22]

source§

impl<T: ToSql + ?Sized> Params for &[(&str, &T); 23]

source§

impl<T: ToSql + ?Sized> Params for &[(&str, &T); 24]

source§

impl<T: ToSql + ?Sized> Params for &[(&str, &T); 25]

source§

impl<T: ToSql + ?Sized> Params for &[(&str, &T); 26]

source§

impl<T: ToSql + ?Sized> Params for &[(&str, &T); 27]

source§

impl<T: ToSql + ?Sized> Params for &[(&str, &T); 28]

source§

impl<T: ToSql + ?Sized> Params for &[(&str, &T); 29]

source§

impl<T: ToSql + ?Sized> Params for &[(&str, &T); 30]

source§

impl<T: ToSql + ?Sized> Params for &[(&str, &T); 31]

source§

impl<T: ToSql + ?Sized> Params for &[(&str, &T); 32]

source§

impl<T: ToSql + ?Sized> Params for &[&T; 1]

source§

impl<T: ToSql + ?Sized> Params for &[&T; 2]

source§

impl<T: ToSql + ?Sized> Params for &[&T; 3]

source§

impl<T: ToSql + ?Sized> Params for &[&T; 4]

source§

impl<T: ToSql + ?Sized> Params for &[&T; 5]

source§

impl<T: ToSql + ?Sized> Params for &[&T; 6]

source§

impl<T: ToSql + ?Sized> Params for &[&T; 7]

source§

impl<T: ToSql + ?Sized> Params for &[&T; 8]

source§

impl<T: ToSql + ?Sized> Params for &[&T; 9]

source§

impl<T: ToSql + ?Sized> Params for &[&T; 10]

source§

impl<T: ToSql + ?Sized> Params for &[&T; 11]

source§

impl<T: ToSql + ?Sized> Params for &[&T; 12]

source§

impl<T: ToSql + ?Sized> Params for &[&T; 13]

source§

impl<T: ToSql + ?Sized> Params for &[&T; 14]

source§

impl<T: ToSql + ?Sized> Params for &[&T; 15]

source§

impl<T: ToSql + ?Sized> Params for &[&T; 16]

source§

impl<T: ToSql + ?Sized> Params for &[&T; 17]

source§

impl<T: ToSql + ?Sized> Params for &[&T; 18]

source§

impl<T: ToSql + ?Sized> Params for &[&T; 19]

source§

impl<T: ToSql + ?Sized> Params for &[&T; 20]

source§

impl<T: ToSql + ?Sized> Params for &[&T; 21]

source§

impl<T: ToSql + ?Sized> Params for &[&T; 22]

source§

impl<T: ToSql + ?Sized> Params for &[&T; 23]

source§

impl<T: ToSql + ?Sized> Params for &[&T; 24]

source§

impl<T: ToSql + ?Sized> Params for &[&T; 25]

source§

impl<T: ToSql + ?Sized> Params for &[&T; 26]

source§

impl<T: ToSql + ?Sized> Params for &[&T; 27]

source§

impl<T: ToSql + ?Sized> Params for &[&T; 28]

source§

impl<T: ToSql + ?Sized> Params for &[&T; 29]

source§

impl<T: ToSql + ?Sized> Params for &[&T; 30]

source§

impl<T: ToSql + ?Sized> Params for &[&T; 31]

source§

impl<T: ToSql + ?Sized> Params for &[&T; 32]

source§

impl<T: ToSql> Params for [T; 1]

source§

impl<T: ToSql> Params for [T; 2]

source§

impl<T: ToSql> Params for [T; 3]

source§

impl<T: ToSql> Params for [T; 4]

source§

impl<T: ToSql> Params for [T; 5]

source§

impl<T: ToSql> Params for [T; 6]

source§

impl<T: ToSql> Params for [T; 7]

source§

impl<T: ToSql> Params for [T; 8]

source§

impl<T: ToSql> Params for [T; 9]

source§

impl<T: ToSql> Params for [T; 10]

source§

impl<T: ToSql> Params for [T; 11]

source§

impl<T: ToSql> Params for [T; 12]

source§

impl<T: ToSql> Params for [T; 13]

source§

impl<T: ToSql> Params for [T; 14]

source§

impl<T: ToSql> Params for [T; 15]

source§

impl<T: ToSql> Params for [T; 16]

source§

impl<T: ToSql> Params for [T; 17]

source§

impl<T: ToSql> Params for [T; 18]

source§

impl<T: ToSql> Params for [T; 19]

source§

impl<T: ToSql> Params for [T; 20]

source§

impl<T: ToSql> Params for [T; 21]

source§

impl<T: ToSql> Params for [T; 22]

source§

impl<T: ToSql> Params for [T; 23]

source§

impl<T: ToSql> Params for [T; 24]

source§

impl<T: ToSql> Params for [T; 25]

source§

impl<T: ToSql> Params for [T; 26]

source§

impl<T: ToSql> Params for [T; 27]

source§

impl<T: ToSql> Params for [T; 28]

source§

impl<T: ToSql> Params for [T; 29]

source§

impl<T: ToSql> Params for [T; 30]

source§

impl<T: ToSql> Params for [T; 31]

source§

impl<T: ToSql> Params for [T; 32]

source§

impl<T: ToSql> Params for (T,)

Implementors§

source§

impl<I> Params for ParamsFromIter<I>
where I: IntoIterator, I::Item: ToSql,