diff --git a/Cargo.toml b/Cargo.toml index c248b60..511f992 100644 --- a/Cargo.toml +++ b/Cargo.toml @@ -6,6 +6,8 @@ edition = "2021" # See more keys and their definitions at https://doc.rust-lang.org/cargo/reference/manifest.html [dependencies] +env_logger = "0.11.3" +log = "0.4.22" sea-orm = {version = "0.12.15",features = [ "sqlx-sqlite", "runtime-tokio-native-tls", "macros" ,"debug-print"] } # serde = "1.0.204" # serde_json = "1.0.120" diff --git a/src/main.rs b/src/main.rs index caab528..ba3e802 100644 --- a/src/main.rs +++ b/src/main.rs @@ -1,6 +1,6 @@ use std::error::Error; -use sea_orm::{ ActiveModelTrait, ActiveValue}; use start_db::Starter; +use log::{error, info}; // use serde_json::json; use tokio; @@ -9,20 +9,17 @@ pub mod start_db; #[tokio::main] async fn main() -> Result<(), Box> { - let db_info = Starter::connect().await?; - db_info.create_table().await.unwrap_or_else(|e: Box|{ - println!("The table have been created.\nDon't care this Error:{}",e); - }); - - let insert_word = word_entity::ActiveModel{ - id:ActiveValue::not_set(), - bsm: ActiveValue::set("test".to_string()), - hz: ActiveValue::set("test".to_string()), - py_js: ActiveValue::set("test".to_string()), - }; + env_logger::init(); + println!("The program is starting.\nIf you want to see the log info,please set env RUST_LOG=info"); + info!("DataBase is starting"); + let db_info = Starter::connect("sqlite://test.db?mode=rwc").await?; - println!("inserting tests into words table in tests.db"); - insert_word.insert(&db_info.db).await?; + db_info.create_table().await.unwrap_or_else(|e: Box|{ + info!("The table have been created. Don't care this Error!!!!!:{}",e); + }); + word_entity::tests::insert_tests_value(&db_info.conn).await.unwrap_or_else(|e|{ + error!("Cannot write data into database.The details are as follows:\n{}",e); + }); Ok(()) } diff --git a/src/start_db.rs b/src/start_db.rs index aa54274..9084ceb 100644 --- a/src/start_db.rs +++ b/src/start_db.rs @@ -3,20 +3,20 @@ use std::error::Error; use sea_orm::Schema; pub struct Starter { - pub db: DatabaseConnection, + pub conn: DatabaseConnection, } impl Starter { - pub async fn connect() -> Result> { - let opt: ConnectOptions = ConnectOptions::new("sqlite://test.db?mode=rwc"); - let db_new: DatabaseConnection = Database::connect(opt).await?; + pub async fn connect(db_url:&str) -> Result> { + let opt: ConnectOptions = ConnectOptions::new(db_url); + let conn_new: DatabaseConnection = Database::connect(opt).await?; - Ok(Starter{db:db_new}) + Ok(Starter{conn:conn_new}) } pub async fn create_table(&self) -> Result<(), Box> { - let backend = self.db.get_database_backend(); + let backend = self.conn.get_database_backend(); let schema = Schema::new(backend); let table_create_statement = schema.create_table_from_entity(super::word_entity::Entity); - let table_create_result = self.db.execute(backend.build(&table_create_statement)).await?; + let table_create_result = self.conn.execute(backend.build(&table_create_statement)).await?; println!("{:?}",table_create_result); Ok(()) } diff --git a/src/test_db.rs b/src/test_db.rs new file mode 100644 index 0000000..e69de29 diff --git a/src/word_entity.rs b/src/word_entity.rs index d2919eb..fadb289 100644 --- a/src/word_entity.rs +++ b/src/word_entity.rs @@ -1,4 +1,5 @@ use sea_orm::entity::prelude::*; +pub mod tests; #[derive(Clone, Debug, PartialEq, Eq, DeriveEntityModel)] #[sea_orm(table_name = "words")] diff --git a/src/word_entity/tests.rs b/src/word_entity/tests.rs new file mode 100644 index 0000000..4413baf --- /dev/null +++ b/src/word_entity/tests.rs @@ -0,0 +1,19 @@ +use std::error::Error; +use log::info; +use sea_orm::{ActiveModelTrait, ActiveValue, DatabaseConnection}; +use super::ActiveModel; + + +pub async fn insert_tests_value(conn:&DatabaseConnection)->Result<(),Box>{ + let insert_word = ActiveModel{ + id:ActiveValue::not_set(), + bsm: ActiveValue::set("test".to_string()), + hz: ActiveValue::set("test".to_string()), + py_js: ActiveValue::set("test".to_string()), + }; + + info!("Inserting tests into words table in tests.db"); + insert_word.insert(conn).await?; + info!("Insert done.Test done"); + Ok(()) +} \ No newline at end of file