Rust: sqlite3 のデータを作成 (Create)
フォルダー構造
$ tree -L 2
.
├── Cargo.lock
├── Cargo.toml
├── src
│ └── main.rs
└── target
└── debug
Cargo.toml
[package]
name = "sqlite3_create"
version = "0.1.0"
edition = "2018"
[dependencies]
rusqlite = "*"
src/main.rs
// --------------------------------------------------------------------
/*
sqlite3_create/src/main.rs
Jul/23/2020
*/
// --------------------------------------------------------------------
use std::env;
use rusqlite::{params, Connection, Result};
// --------------------------------------------------------------------
fn insert_proc(conn:&Connection,key:&'static str,name:&'static str,population: i32,date_mod:&'static str) -> Result<()> {
conn.execute(
"INSERT INTO cities (id,name, population,date_mod) VALUES (?1,?2,$3,$4)",
params![key.to_string(),name.to_string(),population,date_mod.to_string()],
)?;
Ok(())
}
// --------------------------------------------------------------------
fn main() -> Result<()> {
eprintln! ("*** start ***");
let args: Vec<_> = env::args().collect();
let file_sqlite3 = &args[1];
eprintln!("{}",file_sqlite3);
let conn = Connection::open(file_sqlite3).unwrap();
conn.execute(
"drop table cities",
params![],
)?;
conn.execute(
"CREATE TABLE cities (
id VARCHAR(10) PRIMARY KEY,
name TEXT NOT NULL,
population INTEGER,
date_mod VARCHAR(20)
)",
params![],
)?;
let _ = insert_proc(&conn,"t0711","郡山",38415,"1956-5-15");
let _ = insert_proc(&conn,"t0712","会津若松",57926,"1956-6-8");
let _ = insert_proc(&conn,"t0713","白河",98123,"1956-8-17");
let _ = insert_proc(&conn,"t0714","福島",63259,"1956-1-21");
let _ = insert_proc(&conn,"t0715","喜多方",21748,"1956-9-12");
let _ = insert_proc(&conn,"t0716","二本松",51924,"1956-7-5");
let _ = insert_proc(&conn,"t0717","いわき",97125,"1956-9-12");
let _ = insert_proc(&conn,"t0718","相馬",63219,"1956-3-22");
let _ = insert_proc(&conn,"t0719","須賀川",25781,"1956-11-30");
eprintln! ("*** end ***");
Ok(())
}
// --------------------------------------------------------------------
実行
$ cargo run /var/tmp/sqlite3/cities.db
Finished dev [unoptimized + debuginfo] target(s) in 0.80s
Running `target/debug/sqlite3_create /var/tmp/sqlite3/cities.db`
*** start ***
/var/tmp/sqlite3/cities.db
*** end ***
Author And Source
この問題について(Rust: sqlite3 のデータを作成 (Create)), 我々は、より多くの情報をここで見つけました https://qiita.com/ekzemplaro/items/9b79fd54cc3b7b461b78著者帰属:元の著者の情報は、元のURLに含まれています。著作権は原作者に属する。
Content is automatically searched and collected through network algorithms . If there is a violation . Please contact us . We will adjust (correct author information ,or delete content ) as soon as possible .