#unable to open database file (code 14)

8 messages · Page 1 of 1 (latest)

hallow nest
#

So I've read some related threads to this problem, but I need some help making app directory specific locations during production. I've moved my database from src-tauri to src-tauri/resources for development and pre-population. If I understand correctly resources is read only, so specifying a specific location on the users machine is better. For macOs and Windows respectively where are the best places to store the copied DB to be hidden from the user?

Also I am using the SQLite plugin not the Official one.

tawdry mountain
#

app.path_resolver().app_data_dir() this? But this directory will not be created automatically, maybe I haven't found a way?
below is my code

app.path_resolver().app_data_dir().map(|path| {
    if let Ok(false) = path.try_exists() {
        if let Err(err) = std::fs::create_dir(path) {
            error!("Failed to create data dir. {err}");
        }
    }
});
hallow nest
#

Ahh, I am using the webview for my SQL. A Rust refactor is in the works.

#

did you implement a database in your project?

tawdry mountain
hallow nest
#

so the basic idea is copy the database and write it in the app_dir then open the DB from there.

tawdry mountain
#

i think so

hallow nest
# tawdry mountain i think so

I've expanded it to this:


fn setup_database(app: &mut tauri::App) -> Result<(), Box<dyn error::Error>> {
    let db_name = "sqlite-internal.db";
    let data_dir = app_dir_insert(db_name, app)?
    let db_exists = std::path::Path::new(&data_dir).exists();
    match db_exists {
        true => {
            println!("DB Found?: {db_exists}");
            Ok(())
        }
        false => {
            let db_path = find_resource("resources/sqlite-internal.db", app)?;
            fs::copy(&db_path, &data_dir)?;
            Ok(())
        }
    }
}

fn app_dir_insert(insert_path: &str, app: &mut tauri::App) -> Result<PathBuf, io::Error> {
    match app
        .path_resolver()
        .app_data_dir()
        .map(|mut path| match path.try_exists() {
            Ok(true) => {
                path.push(insert_path);
                println!("Full Path {}", path.to_str().unwrap());
                Ok(path)
            }
            Ok(false) => {
                fs::create_dir(path)?;
                path.push(insert_path);
                println!("Full Path {}", path.to_str().unwrap());
                Ok(path)
            }
            Err(path_err) => Err(path_err),
        }) {
        Some(generated_path) => generated_path,
        None => Err(io::Error::new()),
    }
}

fn find_resource(resource_path: &str, app: &mut tauri::App) -> Result<PathBuf, io::Error> {
    match app.path_resolver().resolve_resource(resource_path) {
        Some(path) => match path.try_exists() {
            Ok(true) => {
                println!("Path does exist: {}", path.to_str().unwrap());
                Ok(path)
            }
            Ok(false) => {
                //Potentially download the database from the server.
                unimplemented!("Database resource does not exist");
            }
            Err(path_err) => Err(path_err),
        },
        None => Err(io::Error::new()),
    }
}