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


フォルダー構造

$ tree -L 2
.
├── Cargo.lock
├── Cargo.toml
├── src
│   └── main.rs
└── target
    └── debug
Cargo.toml
[package]
name = "sqlite3_delete"
version = "0.1.0"
edition = "2018"

[dependencies]
rusqlite = "*"
src/main.rs
// --------------------------------------------------------------------
/*
    sqlite3_delete/src/main.rs

                    Jul/23/2020
*/
// --------------------------------------------------------------------
use std::env;
use rusqlite::{params, Connection, Result};

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

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

    eprintln!("{}",file_sqlite3);
    eprintln!("{}",key);

    let conn = Connection::open(file_sqlite3).unwrap();

    let sql_str = "delete from cities where id = '".to_owned() + key + "'";

    conn.execute(&sql_str,params![],)?;

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

    Ok(())
}

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

実行

$ cargo run /var/tmp/sqlite3/cities.db t0716
    Finished dev [unoptimized + debuginfo] target(s) in 0.92s
     Running `target/debug/sqlite3_delete /var/tmp/sqlite3/cities.db t0716`
*** start ***
/var/tmp/sqlite3/cities.db
t0716
*** end ***