Sqlite3, OperationalError: unable to open database file
Solution 1:
Primary diagnosis: SQLite is unable to open that file for some reason.
Checking the obvious reasons why, and in approximate order that I recommend checking:
- Is the program running on the same machine as you're testing it?
- Is it running as you (or at least the same user as you're testing it as)?
- Is the disk containing
/tmp
full? (You're on Unix, so usedf /tmp
to find out.) - Does the
/tmp/cer
directory have “odd” permissions? (SQLite needs to be able to create additional files in it in order to handle things like the commit log.) - Is the unit test code still using that database? (Concurrent opens are possible with a modern-enough SQLite and when in the right filesystem — though
/tmp
is virtually always on the right sort of FS so it's probably not that — but it's still not recommended.) - Is the development code really trying to write to that database, or is something “clever” catching you out and causing it to try to open something else? (I've been caught out by this in my code in the past; don't think it can't happen to you…)
- Are you using the same version of the SQLite library in the unit tests and the production code?
If you're not on the same machine, it's quite possible that the production system doesn't have a /tmp/cer
directory. Obvious to fix that first. Similarly, if you're on the same machine but running as different users, you're likely to have permissions/ownership problems. Disk space is another serious gotcha, but less likely. I don't think it's the last three, but they're worth checking if the more obvious deployment problems are sorted. If it's none of the above, you've hit an exotic problem and will have to report much more info (it might even be a bug in SQLite, but knowing the developers of it, I believe that to be quite unlikely).
Solution 2:
This worked for me:
conn = sqlite3.connect("C:\\users\\guest\\desktop\\example.db")
Note: Double slashes in the full path
Using python v2.7 on Win 7 enterprise and Win Xp Pro
Hope this helps someone.
Solution 3:
On unix I got that error when using the ~
shortcut for the user directory.
Changing it to /home/user
resolved the error.