0
0

Delete article

Deleted articles cannot be recovered.

Draft of this article would be also deleted.

Are you sure you want to delete this article?

More than 3 years have passed since last update.

Rust: PostgreSQL のデータを読む (Read)

Last updated at Posted at 2020-07-24
Cargo.toml
[package]
name = "postgres_read"
version = "0.1.0"
edition = "2018"

[dependencies]
postgres = "*"
src/main.rs
// --------------------------------------------------------------------
/*
	postgres_read/src/main.rs

					Jul/24/2020
*/
// --------------------------------------------------------------------
use postgres::{Client, NoTls, Error};

// --------------------------------------------------------------------
fn main() -> Result<(), Error> {
	eprintln!("*** start ***");
	let mut client = Client::connect("postgresql://scott:tiger123@localhost/city", NoTls)?;

	for row in client.query("SELECT id,name,population,date_mod FROM cities", &[])? {
		let id:String = row.get(0);
		let name:String = row.get(1);
		let population:i32 = row.get(2);
		let date_mod:String = row.get(3);
		println!("{}\t{}\t{}\t{}",id,name,population,date_mod);
	};	

	eprintln!("*** end ***");
	Ok(())

}

// --------------------------------------------------------------------

実行コマンド

cargo run
0
0
0

Register as a new user and use Qiita more conveniently

  1. You get articles that match your needs
  2. You can efficiently read back useful information
  3. You can use dark theme
What you can do with signing up
0
0

Delete article

Deleted articles cannot be recovered.

Draft of this article would be also deleted.

Are you sure you want to delete this article?