Created
April 23, 2015 08:29
-
-
Save arossouw/16288473cbcccc4d4522 to your computer and use it in GitHub Desktop.
Newbie simple select query - mysql-simple
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
extern crate mysql; | |
extern crate time; | |
use mysql::conn::{MyOpts}; | |
use mysql::value::Value; | |
use mysql::conn::pool::{MyPool}; | |
use mysql::value::{from_value}; | |
use std::default::{Default}; | |
use std::vec::Vec; | |
struct Payment { | |
customer_id : i32, | |
amount : f32 | |
} | |
fn main() | |
{ | |
let opts = MyOpts{ | |
user: Some("mysqluser".to_string()), | |
pass: Some("mysqluser".to_string()), | |
db_name: Some("dbsales".to_string()), | |
tcp_addr: Some("10.0.0.82".to_string()), | |
..Default::default() | |
}; | |
let pool = MyPool::new(opts).unwrap(); | |
pool.prepare("SELECT customer_id,amount from payment LIMIT 5").map(|mut stmt| { | |
let mut result = stmt.execute(&[]).unwrap(); | |
for row in result { | |
let payment = Payment { | |
customer_id : row[0], | |
amount : row[1] | |
}; | |
//assert_eq!(row.unwrap(), vec![Value::Int(42)]); | |
println!("Found payment {} {}" , payment.customer_id, payment.amount); | |
} | |
}); | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment