Question: Why can't I open the database?
Info: I'm working on a project using sqlite3
database. I wrote a test program that runs and passes it the database:
/tmp/cer/could.db
The unit test program can make the db
without any problem. But, when I actually use the program passing the same location to it, i got below error:
OperationalError: unable to open database file
I've tried doing it with:
1) an empty database.
2) the database and the unit test left behind.
3) no database at all.
In three cases, I got the above error. The most frustrating part has to be the fact that the unittest
can do it just fine, but the actual program can't.
Any clues as to what on earth is going on?
Ran into this issue while trying to create an index on a perfectly valid database. Turns out it will throw this error (in addition to other reasons described here) if the sqlite
temp_store_directory
variable/directory is unwritable.Solution: change
temp_store_directory
withc.execute(f'PRAGMA temp_store_directory = "{writable_directory}"')
. Note that this pragma is being deprecated and I am not yet sure what the replacement will be.for clearer full path if you didn't get it clear
Run into the error on Windows, added assert os.path.exists, double checked the path, run the script as administrator, nothing helped.
Turns out if you add your folders to the Windows Defender's Ransomware Protection, you can no longer use other programs to write there unless you add these programs to the Controlled Folder Access' whitelist.
Solution - check if your folder has been added to the Windows Defender's Ransomware Protection and remove it for faster fix.
The only thing you need to do is create the folder (as it doesn't exist already), only the database file will be created by the program. This really worked for me!
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:
/tmp
full? (You're on Unix, so usedf /tmp
to find out.)/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.)/tmp
is virtually always on the right sort of FS so it's probably not that — but it's still not recommended.)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).