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: sqlite3 のデータを更新 (Update)

Posted at

フォルダー構造

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

[dependencies]
rusqlite = "*"
chrono = "0.4"
src/main.rs
// --------------------------------------------------------------------
/*
	sqlite3_update/src/main.rs

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

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

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

	eprintln!("{}",file_sqlite3);
	eprintln!("{}",key_in);
	eprintln!("{}",population_in);

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

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

	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 + "'";

	eprintln!("{}",sql_str);

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

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

	Ok(())
}

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

実行

$ cargo run /var/tmp/sqlite3/cities.db t0713 3512600
    Finished dev [unoptimized + debuginfo] target(s) in 1.24s
     Running `target/debug/sqlite3_update /var/tmp/sqlite3/cities.db t0713 3512600`
*** start ***
/var/tmp/sqlite3/cities.db
t0713
3512600
2020-07-23+09:00
update cities set population = 3512600, date_mod = '2020-07-23+09:00' where id = 't0713'
*** end ***
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?