sqlx
sqlx copied to clipboard
'unable to open database file' when running the sqlite/todos example
When following the exact steps given in the sqlite todos example, I get several of these errors when trying to execute cargo run -- add "todo description"
:
error: error returned from database: unable to open database file
--> examples/sqlite/todos/src/main.rs:80:16
|
80 | let recs = sqlx::query!(
At the time of the execution of the macro query::expand_from_db, a std::env::current_dir()
, that I inserted for debugging, returns the root directory of the sqlx repository, and not the sqlx/examples/sqlite/todos directory, which is where I'm running the example from. The macro attempts to open the sqlite database relative to the repository root, and fails.
Hi, have you figured out how to run the todo example?
Hi, as mentioned in this comment, you can make it work by using an absolute DB URL:
export DATABASE_URL="sqlite:/YOUR ABSOLUTE PATH/sqlx/examples/sqlite/todos/todos.db"
Relative URLs should work, I'm not really sure why they don't sometimes.
I ran into this problem too, and it was a similar situation where the crate that used sqlx was not the root dir. I was able to work around it, and still use a relative URL, by creating a symlink to the sqlite DB in the root dir.
E.g.:
- fancy-cargo-workspace/
- Cargo.toml
- sqlite.db -> crate-that-uses-sqlx/sqlite.db (this is a symlink)
- crate-that-uses-sqlx/
- sqlite.db (this is the ordinary DB used for migrations and compilation)
- .env
- Cargo.toml
- build.rs
- src/
- migrations/
I have encountered similar problems, but I am Windows 10
DATABASE_URL=sqlite:/Users/Administrator/Desktop/plc/data.db
Superseded by #3099