LoginSignup
0
0

More than 3 years have passed since last update.

Rust: PostgreSQL のデータを更新 (Update)

Posted at
Cargo.toml
[package]
name = "postgres_update"
version = "0.1.0"
edition = "2018"

[dependencies]
postgres = "*"
chrono = "0.4"
src/main.rs
// --------------------------------------------------------------------
/*
    postgres_update/src/main.rs

                    Jul/24/2020
*/
// --------------------------------------------------------------------
use std::env;
use postgres::{Client, NoTls, Error};
use chrono::{Local, Date};

// --------------------------------------------------------------------
fn main() -> Result<(), Error> {
    eprintln!("*** start ***");

    let args: Vec<_> = env::args().collect();
    let key_in = &args[1];
    let population_in = &args[2];
    eprintln!("{}",key_in);
    eprintln!("{}",population_in);

    let mut client = Client::connect("postgresql://scott:tiger123@localhost/city", NoTls)?;

    let date_mod: Date<Local> = Local::today();
    println!("{}", date_mod);

    let sql_str = "update cities set population = ".to_owned()
        + population_in + &", date_mod = '".to_owned()
        + &date_mod.to_string()
        + &"' where id = '".to_owned() + key_in + "'";

    client.batch_execute(&sql_str)?;

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

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

実行コマンド

cargo run t3462 7621300
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