引入crate:
postgres = "0.19.7"
rust">use postgres::{Client, NoTls, error::Error};
fn main() -> Result<(), Error> {
let mut client = Client::connect("host=localhost port=5432 dbname=xxxxdb user=postgres password=xxxxxx", NoTls).unwrap();
for row in client.query("select * from xxxx", &[])?{
let id: i32 = row.get(0);
let name: &str = row.get(1);
let file_name: &str = row.get(2);
//let create_time: std::time = row.get(3);
let valid: bool = row.get(4);
println!("id:{}, name:{}, file_name:{}, valid:{}", id, name, file_name, valid);
}
Ok(())
}
注意:用户名不要使用root。否则报错:
thread 'main' panicked at src\main.rs:5:115:
called `Result::unwrap()` on an `Err` value: Error { kind: Parse, cause: Some(Custom { kind: InvalidInput, error: Utf8Error { valid_up_to: 0, error_len: Some(1) } }) }