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();
|
2015-06-01 16:27:09 -04:00
|
|
|
let database = sqlite::open(&path).unwrap();
|
2015-05-29 09:25:39 -04:00
|
|
|
|
2015-06-08 15:00:44 -04:00
|
|
|
database.execute(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');
|
2015-06-08 13:27:07 -04:00
|
|
|
"#).unwrap();
|
2015-05-29 09:25:39 -04:00
|
|
|
|
2015-06-08 15:00:44 -04:00
|
|
|
database.process("SELECT * FROM `users`;", |pairs| {
|
2015-06-08 21:42:34 -04:00
|
|
|
for &(column, value) in pairs.iter() {
|
|
|
|
println!("{} = {}", column, value.unwrap());
|
2015-05-29 09:25:39 -04:00
|
|
|
}
|
|
|
|
true
|
2015-06-08 13:27:07 -04:00
|
|
|
}).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
|
|
|
|
}
|