49 lines
1.3 KiB
Rust
49 lines
1.3 KiB
Rust
use rusqlite::{params, Connection, Result};
|
|
use time::Timespec;
|
|
|
|
#[derive(Debug)]
|
|
struct Person {
|
|
id: i32,
|
|
name: String,
|
|
time_created: Timespec,
|
|
memo: String,
|
|
}
|
|
|
|
fn main() -> Result<()> {
|
|
let conn = Connection::open_in_memory()?;
|
|
|
|
conn.execute(
|
|
r#"CREATE TABLE person (
|
|
id INTEGER PRIMARY KEY,
|
|
name TEXT NOT NULL,
|
|
time_created TEXT NOT NULL,
|
|
memo TEXT NOT NULL
|
|
)"#,
|
|
params![],
|
|
)?;
|
|
let me = Person {
|
|
id: 0,
|
|
name: "Steven".to_owned(),
|
|
time_created: time::get_time(),
|
|
memo: "memo...".to_owned(),
|
|
};
|
|
conn.execute(
|
|
"INSERT INTO person (name, time_created, memo) VALUES (?1, ?2, ?3)",
|
|
params![me.name, me.time_created, me.memo],
|
|
)?;
|
|
|
|
let mut stmt = conn.prepare("SELECT id, name, time_created, memo FROM person")?;
|
|
let person_iter = stmt.query_map(params![], |row| {
|
|
Ok(Person {
|
|
id: row.get(0)?,
|
|
name: row.get(1)?,
|
|
time_created: row.get(2)?,
|
|
memo: row.get(3)?,
|
|
})
|
|
})?;
|
|
|
|
for person in person_iter {
|
|
println!("Found person {:?}", person.unwrap());
|
|
}
|
|
Ok(())
|
|
} |