- prepare
- bind
- next
SQLite INSERT with placeholder
- We can (and should) use placeholders in INSERT statements as well
examples/sqlite/in-memory-insert-placeholders/src/main.rs
use sqlite::State; fn main() { let connection = sqlite::open(":memory:").unwrap(); connection .execute("CREATE TABLE users (name TEXT, age INTEGER, grade INTEGER);") .unwrap(); let mut insert_statement = connection .prepare("INSERT INTO users VALUES (:name, :age, :grade);") .unwrap(); //insert_statement match insert_statement.bind_iter::<_, (_, sqlite::Value)>([ (":name", "Mary".into()), (":age", 100.into()), (":grade", 99.into()), ]) { Ok(val) => println!("{:?}", val), Err(err) => println!("{}", err), } let _ = insert_statement.next(); let _ = insert_statement.reset(); match insert_statement.bind_iter::<_, (_, sqlite::Value)>([ (":name", "George".into()), (":age", 99.into()), (":grade", 65.into()), ]) { Ok(val) => println!("{:?}", val), Err(err) => println!("{}", err), } let _ = insert_statement.next(); let query = "SELECT * FROM users"; let mut statement = connection.prepare(query).unwrap(); while let Ok(State::Row) = statement.next() { println!("name = {}", statement.read::<String, _>("name").unwrap()); println!("age = {}", statement.read::<i64, _>("age").unwrap()); println!("grade = {}", statement.read::<i64, _>("grade").unwrap()); } }
() () name = Mary age = 100 grade = 99 name = George age = 99 grade = 65