This commit is contained in:
Zengtudor 2024-07-20 00:17:57 +08:00
parent a6736aec16
commit 49662e68d5
5 changed files with 95 additions and 0 deletions

6
.gitignore vendored
View File

@ -14,3 +14,9 @@ Cargo.lock
# MSVC Windows builds of rustc generate these, which store debugging information
*.pdb
# Added by cargo
/target
test.db

17
Cargo.toml Normal file
View File

@ -0,0 +1,17 @@
[package]
name = "sea_orm"
version = "0.1.0"
edition = "2021"
# See more keys and their definitions at https://doc.rust-lang.org/cargo/reference/manifest.html
[dependencies]
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"
tokio = {version = "1.38.1",features = ["full"]}
[profile.release]
opt-level = 3
lto = true
debug = false

24
src/main.rs Normal file
View File

@ -0,0 +1,24 @@
use std::error::Error;
use sea_orm::{ ActiveModelTrait, ActiveValue};
use start_db::Starter;
// use serde_json::json;
use tokio;
pub mod word_entity;
pub mod start_db;
#[tokio::main]
async fn main() -> Result<(), Box<dyn Error>> {
let db_info = Starter::connect().await?;
// db_info.create_table().await?;
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()),
};
insert_word.insert(&db_info.db).await?;
Ok(())
}

23
src/start_db.rs Normal file
View File

@ -0,0 +1,23 @@
use sea_orm::{Condition, ConnectOptions, ConnectionTrait, Database, DatabaseConnection};
use std::error::Error;
use sea_orm::Schema;
pub struct Starter {
pub db: DatabaseConnection,
}
impl Starter {
pub async fn connect() -> Result<Self, Box<dyn Error>> {
let opt: ConnectOptions = ConnectOptions::new("sqlite://test.db?mode=rwc");
let db_new: DatabaseConnection = Database::connect(opt).await?;
Ok(Starter{db:db_new})
}
pub async fn create_table(&self) -> Result<(), Box<dyn Error>> {
let backend = self.db.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?;
println!("{:?}",table_create_result);
Ok(())
}
}

25
src/word_entity.rs Normal file
View File

@ -0,0 +1,25 @@
use sea_orm::entity::prelude::*;
#[derive(Clone, Debug, PartialEq, Eq, DeriveEntityModel)]
#[sea_orm(table_name = "words")]
pub struct Model{
#[sea_orm(primary_key)]
pub id: i32,
pub bsm: String,
pub hz:String,
pub py_js: String,
}
#[derive(Copy, Clone, Debug, EnumIter, DeriveRelation)]
pub enum Relation {
// #[sea_orm(has_many = "super::fruit::Entity")]
// Fruit,
}
// impl Related<super::fruit::Entity> for Entity {
// fn to() -> RelationDef {
// Relation::Fruit.def()
// }
// }
impl ActiveModelBehavior for ActiveModel {}