Bindable

Trait Bindable 

Source
pub trait Bindable
where 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§

Source§

impl Bindable for f64

Bindable implementation 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);
}
Source§

impl Bindable for i64

Bindable implementation 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);
}
Source§

impl Bindable for str

Bindable implementation for str slices.

§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);
}
Source§

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);
}
Source§

impl<T> Bindable for Option<T>
where T: Bindable,

Bindable implementation for Option.

§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))]);
Source§

impl<T> Bindable for &T
where T: ?Sized + Bindable,

Source§

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§

Source§

impl Bindable for Null

Bindable implementation 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")]);
Source§

impl Bindable for Value

Bindable implementation for a dynamic 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")]);