sqlx icon indicating copy to clipboard operation
sqlx copied to clipboard

'unable to open database file' when running the sqlite/todos example

Open ousado opened this issue 3 years ago • 2 comments

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.

This is using the 0.5.5 release

ousado avatar May 28 '21 18:05 ousado

Hi, have you figured out how to run the todo example?

Yevgnen avatar Aug 10 '22 09:08 Yevgnen

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"

jojva avatar Sep 13 '22 14:09 jojva

Relative URLs should work, I'm not really sure why they don't sometimes.

abonander avatar Feb 01 '23 22:02 abonander

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/

vdods avatar Feb 17 '23 07:02 vdods

I have encountered similar problems, but I am Windows 10 DATABASE_URL=sqlite:/Users/Administrator/Desktop/plc/data.db

Hans-Wu-cn avatar Mar 16 '23 15:03 Hans-Wu-cn

Superseded by #3099

abonander avatar Mar 06 '24 09:03 abonander