pub trait Params {
// Required method
fn bind_params(self, stmt: &mut Statement) -> Result<()>;
}
Expand description
Trait for collections of parameters to a query.
This is a private trait with no public API. There are existing implementations which should cover most use cases:
- An empty tuple (
()
) binds no parameters to the query. - An array binds parameters that are all the same type.
- The params! macro binds parameters of arbitrary types.
- A closure can arbitrarily bind parameters.
Named parameters are implemented by using a tuple of ("name", value)
, and can be in any
order. See params! for an example.
§Using a closure
If you are dynamically creating SQL queries and need to dynamically bind parameters to them, you can use a closure to accomplish this.
use sqlite3_ext::{Connection, Result, query::{ Statement, ToParam }};
fn do_thing(conn: &Connection) -> Result<i64> {
conn.prepare("INSERT INTO tbl VALUES (?, ?)")?
.execute(|stmt: &mut Statement| {
"foo".bind_param(stmt, 1)?;
"bar".bind_param(stmt, 2)?;
Ok(())
})
}