Last active
September 27, 2020 02:19
-
-
Save kuc-arc-f/b172fb1f8f043e66773094146cd50bc8 to your computer and use it in GitHub Desktop.
Rust, 起動時の引数から sqlite3 登録する処理
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
[package] | |
name = "sqlite-test" | |
version = "0.1.0" | |
authors = ["naka"] | |
edition = "2018" | |
# See more keys and their definitions at https://doc.rust-lang.org/cargo/reference/manifest.html | |
[dependencies] | |
rusqlite = "0.24.0" | |
serde = { version = "1.0", features = ["derive"] } | |
serde_json = "1.0" | |
chrono = "0.4.15" |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
// Rust, 起動時の引数から sqlite3 登録する処理 | |
use rusqlite::{params, Connection, Result}; | |
use std::env; | |
use std::fs::File; | |
use std::io::prelude::*; | |
use serde::{Deserialize, Serialize}; | |
use chrono::{Utc, Local, DateTime, Date}; | |
use chrono::{NaiveDateTime, Duration}; | |
// | |
pub fn get_content(filename: String ) -> String{ | |
// println!("In file {}", filename); | |
let mut f = File::open(filename).expect("file not found"); | |
let mut contents = String::new(); | |
f.read_to_string(&mut contents) | |
.expect("something went wrong reading the file"); | |
// println!("With text:\n{}", contents); | |
return contents; | |
} | |
#[derive(Debug)] | |
pub struct Person { | |
id: i32, | |
name: String, | |
data: Option<Vec<u8>>, | |
} | |
// | |
fn exec_sql_array(file_sqlite3: String, items: Vec<TaskItem>) { | |
let conn = Connection::open(file_sqlite3).unwrap(); | |
for row in &items { | |
// println!("exe.name={}", &row.name ); | |
let s_title = &row.title; | |
let s_content = &row.content; | |
let me = TaskItem { | |
id: 0, | |
title: s_title.to_string(), | |
content: s_content.to_string(), | |
}; | |
conn.execute( | |
"INSERT INTO tasks (title, content) VALUES (?1, ?2)", | |
params![ me.title, me.content ], | |
).unwrap(); | |
} | |
// Ok(()) | |
} | |
// | |
#[derive(Serialize, Deserialize , Debug)] | |
struct TestItem { | |
age: i64, | |
name: String, | |
} | |
// | |
#[derive(Serialize, Deserialize , Debug)] | |
struct TaskItem { | |
id: i64, | |
title: String, | |
content: String, | |
} | |
// | |
pub fn exec_main(json_fname: String, dbfname: String){ | |
let text = Utc::now().format("%Y-%m-%d %H:%M:%S.%f").to_string(); | |
println!("text_1={}", text); | |
let dt1: NaiveDateTime = NaiveDateTime::parse_from_str( &text, "%Y-%m-%d %H:%M:%S.%f").unwrap(); | |
// let json_fname = "/home/naka/work/node/express/app7/public/tasks.json"; | |
let json = get_content( json_fname ); | |
// println!("main={}", json); | |
let deserialized: Vec<TaskItem> = serde_json::from_str(&json).unwrap(); | |
let dbfname_str = String::from(dbfname); | |
// let dbfname_str = String::from("/home/naka/work/node/express/app7/public/test.db"); | |
exec_sql_array(dbfname_str , deserialized); | |
let text_2 = Utc::now().format("%Y-%m-%d %H:%M:%S.%f").to_string(); | |
let dt2: NaiveDateTime = NaiveDateTime::parse_from_str( &text_2, "%Y-%m-%d %H:%M:%S.%f" ).unwrap(); | |
let duration: Duration = dt2 - dt1; | |
println!("text_2={}", text_2 ); | |
println!("secs: {}", duration.num_seconds()); | |
println!("milliseconds: {}", duration.num_milliseconds()); | |
} | |
// | |
fn main(){ | |
println!("#-start-main"); | |
let args: Vec<String> = env::args().collect(); | |
let arg1 = &args[1]; | |
let arg2 = &args[2]; | |
println!("{:?}", args); | |
println!("{}", arg1 ); | |
exec_main( arg1.to_string() , arg2.to_string() ); | |
// test1(); | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment