learn-hieroglyphs/src/db.rs

34 lines
1.1 KiB
Rust
Raw Normal View History

2024-04-05 15:01:24 +03:00
use sqlite;
use std::{path::PathBuf, process::Command};
pub fn init() {
let program_home_path = if cfg!(target_os = "windows") {
let binding = std::env::var("APP_DATA").expect("No APP_DATA directory");
let mut appdata = PathBuf::from(&binding);
appdata = appdata.join("learn-hieroglyph");
Command::new("mkdir").arg(&appdata);
appdata
} else {
let binding = std::env::var("HOME").expect("No HOME directory");
let mut home = PathBuf::from(&binding);
home = home.join(".config/learn-hieroglyph");
Command::new("mkdir").args(["-p", &home.to_str().unwrap()]).spawn().expect(&format!("Cannot create program's home: {}", &home.to_str().unwrap()));
home
};
println!("Program's home: {}", program_home_path.as_path().to_str().unwrap());
let connection = sqlite::open(program_home_path.join("db.sqlite")).unwrap();
connection.execute("CREATE TABLE IF NOT EXISTS cards (
id INTEGER PRIMARY KEY AUTOINCREMENT,
image_hash CHAR(64),
hieroglyph VARCHAR(2),
translation VARCHAR(128)
)").unwrap();
}