30 lines
677 B
Rust
Raw Normal View History

2015-05-29 09:25:39 -04:00
extern crate sqlite;
2015-06-08 14:56:54 -04:00
use std::fs;
2015-05-29 09:25:39 -04:00
use std::path::PathBuf;
fn main() {
let path = setup();
let database = sqlite::open(&path).unwrap();
2015-05-29 09:25:39 -04:00
database.instruct(r#"
2015-05-29 09:25:39 -04:00
CREATE TABLE `users` (id INTEGER, name VARCHAR(255));
INSERT INTO `users` (id, name) VALUES (1, 'Alice');
"#).unwrap();
2015-05-29 09:25:39 -04:00
database.iterate("SELECT * FROM `users`;", |pairs| {
2015-05-29 09:25:39 -04:00
for (ref column, ref value) in pairs {
println!("{} = {}", column, value);
}
true
}).unwrap();
2015-05-29 09:25:39 -04:00
}
fn setup() -> PathBuf {
let path = PathBuf::from("database.sqlite3");
2015-06-08 14:56:54 -04:00
if fs::metadata(&path).is_ok() {
fs::remove_file(&path).unwrap();
2015-05-29 09:25:39 -04:00
}
path
}