pub trait Bindablewhere
Self: Sealed,{ }Expand description
A type suitable for binding to a prepared statement.
Use with Statement::bind or Statement::bind_by_name.
Implementations on Foreign Types§
impl Bindable for f64
§Examples
use sqlite_ll::Connection;
let c = Connection::open_memory()?;
c.execute(r#"
CREATE TABLE measurements (value REAL);
INSERT INTO measurements (value) VALUES (3.14), (2.71), (1.61);
"#)?;
let mut stmt = c.prepare("SELECT COUNT(*) FROM measurements WHERE value > ?")?;
stmt.bind(1, 2.0f64)?;
while let Some(row) = stmt.next()? {
assert_eq!(row.read::<i64>(0)?, 2);
}impl Bindable for i64
§Examples
use sqlite_ll::Connection;
let c = Connection::open_memory()?;
c.execute(r#"
CREATE TABLE measurements (value INTEGER);
INSERT INTO measurements (value) VALUES (3), (2), (1);
"#)?;
let mut stmt = c.prepare("SELECT COUNT(*) FROM measurements WHERE value > ?")?;
stmt.bind(1, 2)?;
while let Some(row) = stmt.next()? {
assert_eq!(row.read::<i64>(0)?, 1);
}impl Bindable for str
§Examples
use sqlite_ll::Connection;
let c = Connection::open_memory()?;
c.execute(r##"
CREATE TABLE users (name TEXT, age INTEGER);
INSERT INTO users (name, age) VALUES ('Alice', 42), ('Bob', 30);
"##)?;
let mut stmt = c.prepare("SELECT age FROM users WHERE name = ?")?;
stmt.bind(1, "Alice")?;
while let Some(row) = stmt.next()? {
assert_eq!(row.read::<i64>(0)?, 42);
}impl Bindable for [u8]
Bindable implementation for byte slices.
§Examples
use sqlite_ll::Connection;
let c = Connection::open_memory()?;
c.execute(r##"
CREATE TABLE files (id INTEGER, data BLOB);
INSERT INTO files (id, data) VALUES (0, X'48656C6C6F20576F726C6421');
INSERT INTO files (id, data) VALUES (1, X'48656C6C6F');
"##)?;
let mut stmt = c.prepare("SELECT id FROM files WHERE data = ?")?;
stmt.bind(1, &b"Hello"[..])?;
while let Some(row) = stmt.next()? {
assert_eq!(row.read::<i64>(0)?, 1);
}impl<T> Bindable for Option<T>where
T: Bindable,
§Examples
use sqlite_ll::{Connection, State};
let c = Connection::open_memory()?;
c.execute(r##"
CREATE TABLE users (name TEXT, age INTEGER);
"##)?;
let mut stmt = c.prepare("INSERT INTO users (name, age) VALUES (?, ?)")?;
stmt.reset()?;
stmt.bind(1, "Alice")?;
stmt.bind(2, None::<i64>)?;
assert_eq!(stmt.step()?, State::Done);
stmt.reset()?;
stmt.bind(1, "Bob")?;
stmt.bind(2, Some(30i64))?;
assert_eq!(stmt.step()?, State::Done);
let mut stmt = c.prepare("SELECT name, age FROM users")?;
let mut names_and_ages = Vec::new();
while let State::Row = stmt.step()? {
let name: String = stmt.read(0)?;
let age: Option<i64> = stmt.read(1)?;
names_and_ages.push((name, age));
}
names_and_ages.sort();
assert_eq!(names_and_ages, vec![(String::from("Alice"), None), (String::from("Bob"), Some(30))]);impl<T> Bindable for &T
impl<const N: usize> Bindable for [u8; N]
Bindable implementation for byte arrays.
§Examples
use sqlite_ll::Connection;
let c = Connection::open_memory()?;
c.execute(r##"
CREATE TABLE files (id INTEGER, data BLOB);
INSERT INTO files (id, data) VALUES (0, X'48656C6C6F20576F726C6421');
INSERT INTO files (id, data) VALUES (1, X'48656C6C6F');
"##)?;
let mut stmt = c.prepare("SELECT id FROM files WHERE data = ?")?;
stmt.bind(1, b"Hello")?;
while let Some(row) = stmt.next()? {
assert_eq!(row.read::<i64>(0)?, 1);
}Implementors§
impl Bindable for Null
§Examples
use sqlite_ll::{Connection, Null};
let c = Connection::open_memory()?;
c.execute(r##"
CREATE TABLE users (name TEXT, age INTEGER);
INSERT INTO users (name, age) VALUES ('Alice', NULL), ('Bob', 30);
"##)?;
let mut stmt = c.prepare("SELECT name FROM users WHERE age IS ?")?;
stmt.bind(1, Null)?;
let mut names = Vec::new();
while let Some(row) = stmt.next()? {
names.push(row.read::<String>(0)?);
}
assert_eq!(names, vec![String::from("Alice")]);impl Bindable for Value
§Examples
use sqlite_ll::{Connection, Value};
let c = Connection::open_memory()?;
c.execute(r##"
CREATE TABLE users (name TEXT, age INTEGER);
INSERT INTO users (name, age) VALUES ('Alice', NULL), ('Bob', 30);
"##)?;
let mut stmt = c.prepare("SELECT name FROM users WHERE age IS ?")?;
stmt.bind(1, Value::null())?;
let mut names = Vec::new();
while let Some(row) = stmt.next()? {
names.push(row.read::<String>(0)?);
}
assert_eq!(names, vec![String::from("Alice")]);