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?
I faced the same problem on Windows 7. My database name was
test
and I got the error:I replaced
test
withtest.db
and and all went smooth.My reason was very foolish. I had dropped the manage.py onto the terminal so it was running using the full path. And I had changed the name of the folder of the project. So now, the program was unable to find the file with the previous data and hence the error.
Make sure you restart the software in such cases.
Make sure you are not editing the settings.py file while trying to run syncdb, you will get the same error!!!
1) Verify your database path, check in your settings.py
some times there wont be NAME': os.path.join(BASE_DIR, 'project.db'),
2)Be sure for the permission and ownership to destination folder
it worked for me,
One reason might be running the code in a path that doesn't match with your specified path for the database. For example if in your code you have:
And you run the code inside the
folder_A
or other places that doesn't have afolder_A
it will raise such error. The reason is that SQLite will create the database file if it doesn't exist not the folder.One other way for getting around this problem might be wrapping your connecting command in a
try-except
expression and creating the directory if it raisessqlite3.OperationalError
.from os import mkdir import sqlite3 as lite
If this happens randomly after correctly being able to access your database (and no settings have changed), it could be a result of a corrupted database.
I got this error after trying to write to my database from two processes at the same time and it must have corrupted my db.sqlite3 file.
My solution was to revert back to a previous commit before the corruption happened.