[package]
name = "tokio-psql"
version = "0.1.0"
authors = ["Jinhua Luo <[email protected]>"]
edition = "2018"
[dependencies]
futures-preview = "=0.3.0-alpha.19"
tokio = "0.2.0-alpha.6"
tokio-postgres= { git = "https://github.com/sfackler/rust-postgres" }
--------------
use futures::FutureExt;
use tokio_postgres::{Error, NoTls, Row};
#[tokio::main]
async fn main() -> Result<(), Error> {
// Connect to the database.
let (client, connection) =
tokio_postgres::connect("host=localhost user=postgres", NoTls).await?;
// The connection object performs the actual communication with the database,
// so spawn it off to run on its own.
let connection = connection.map(|r| {
if let Err(e) = r {
eprintln!("connection error: {}", e);
}
});
tokio::spawn(connection);
// Now we can prepare a simple statement that just returns its parameter.
let stmt = client.prepare("SELECT $1::TEXT").await?;
// And then execute it, returning a Stream of Rows which we collect into a Vec.
let rows: Vec<Row> = client.query(&stmt, &[&"hello world"]).await?;
// Now we can check that we got back the same string we sent over.
let value: &str = rows[0].get(0);
assert_eq!(value, "hello world");
Ok(())
}
Comments
0 B
|👍
/👎