1 extern crate rusqlite;
2 use rusqlite::{Connection, Result};
3
4 struct Person {
5 id: i32,
6 name: String,
7 }
main() -> Result<()>8 fn main() -> Result<()> {
9 let conn = Connection::open_in_memory()?;
10
11 conn.execute(
12 "CREATE TABLE IF NOT EXISTS persons (
13 id INTEGER PRIMARY KEY,
14 name TEXT NOT NULL
15 )",
16 (), // empty list of parameters.
17 )?;
18
19 conn.execute(
20 "INSERT INTO persons (name) VALUES (?1), (?2), (?3)",
21 ["Steven", "John", "Alex"].map(|n| n.to_string()),
22 )?;
23
24 let mut stmt = conn.prepare("SELECT id, name FROM persons")?;
25 let rows = stmt.query_map([], |row| {
26 Ok(Person {
27 id: row.get(0)?,
28 name: row.get(1)?,
29 })
30 })?;
31
32 println!("Found persons:");
33
34 for person in rows {
35 match person {
36 Ok(p) => println!("ID: {}, Name: {}", p.id, p.name),
37 Err(e) => eprintln!("Error: {e:?}"),
38 }
39 }
40
41 Ok(())
42 }
43