Created
October 9, 2019 15:30
-
-
Save kingluo/999dd1ec1c2e39291428dd3f7719c334 to your computer and use it in GitHub Desktop.
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
[package] | |
name = "tokio-psql" | |
version = "0.1.0" | |
authors = ["Jinhua Luo <home_king@163.com>"] | |
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" } |
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
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(()) | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
Pointing to the latest git has still compile errors issues
tokio-postgres= { git = "https://github.com/sfackler/rust-postgres" }
.Here is my code
This must be easier to solve since the Error is of type tokio_postgres::error::Error
But i reverted back to
0.4.0-rc3
and downloaded https://github.com/sfackler/rust-postgres/archive/tokio-postgres-v0.4.0-rc.3.tar.gz. Verified their test code in there and used to build our stuff.Will wait for an official build.