fixing database directory error

This commit is contained in:
liyunze 2026-01-06 23:37:41 +08:00
parent d1c8d66cca
commit 79464d3bfb

View file

@ -1,4 +1,4 @@
use std::{env, path::Path};
use std::{env, fs, path::Path};
// Using SQLite
use rusqlite::{Connection, OpenFlags, Result};
@ -7,7 +7,14 @@ pub fn init_db() -> Result<Connection, rusqlite::Error> {
let filename = env::var("DATABASE_PATH").unwrap_or_else(|_| "bot_data.sqlite".to_string());
let filepath = Path::new(&filename);
let open_flags = OpenFlags::default();
if let Some(parent) = filepath.parent() {
if !parent.as_os_str().is_empty() {
fs::create_dir_all(parent).expect("Failed to create database directory");
}
}
let open_flags =
OpenFlags::default() | OpenFlags::SQLITE_OPEN_CREATE | OpenFlags::SQLITE_OPEN_READ_WRITE;
Connection::open_with_flags(filepath, open_flags)
}