Rust: PostgreSQL のデータを削除 (Delete)


Cargo.toml
[package]
name = "postgres_delete"
version = "0.1.0"
edition = "2018"

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

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

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

    let args: Vec<_> = env::args().collect();
    let key_in = &args[1];

    eprintln!("{}",key_in);

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

    let sql_str = "delete from cities where id = '".to_string()
        + &key_in.to_string() + "'";

    client.batch_execute(&sql_str)?;



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

}

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

実行コマンド

cargo run t3467