Sqlite3, OperationalError: unable to open database file
Question:
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?
Answers:
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 use df /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).
Make sure you are not editing the settings.py file while trying to run syncdb, you will get the same error!!!
self.connection = Database.connect(**kwargs)
sqlite3.OperationalError: unable to open database file
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.
I faced the same problem on Windows 7. My database name was test
and I got the error:
self.connection = Database.connect(**kwargs)
sqlite3.OperationalError: unable to open database file
I replaced test
with test.db
and and all went smooth.
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:
conn = lite.connect('folder_A/my_database.db')
And you run the code inside the folder_A
or other places that doesn’t have a folder_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 raises sqlite3.OperationalError
.
from os import mkdir
import sqlite3 as lite
try:
conn = lite.connect('folder_A/my_database.db')
except lite.OperationalError:
mkdir('folder_A')
finally:
conn = lite.connect('folder_A/my_database.db')
On unix I got that error when using the ~
shortcut for the user directory.
Changing it to /home/user
resolved the error.
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!
In my case, the solution was to use an absolute path, to find an existing file:
import os.path
filepath = os.path.abspath(filepath)
# Leave this out if the file doesn't exist yet
assert os.path.exists(filepath), "The file doesn't exist"
conn = sqlite3.connect(filepath)
I don’t know why this fix works: the path only contained ASCII characters and no spaces. Still it made the difference.
For reference: Windows 7, Python 3.6.5 (64-bit).
I was not able to reproduce the issue on another machine (also Windows 7, Python 3.6.4 64-bit), so I have no idea why this fix works.
import sqlite3
connection = sqlite3.connect("d:\pythonAPI\data.db")
cursor = connection.cursor()
create_table = "CREATE TABLE users (id int, username text, password text)"
cursor.execute(create_table)
for clearer full path if you didn’t get it clear
This is definitely a permissions issue. If someone is getting this error on linux, make sure you’re running the command with sudo
as the file most likely is owned by root. Hope that helps!
Use the fully classified name of database file
Use- /home/ankit/Desktop/DS/Week-7-MachineLearning/Week-7-MachineLearning/soccer/database.sqlite
instead-
1) Verify your database path,
check in your settings.py
DATABASES = {
'default': {
'CONN_MAX_AGE': 0,
'ENGINE': 'django.db.backends.sqlite3',
'HOST': 'localhost',
'NAME': os.path.join(BASE_DIR, 'project.db'),
'PASSWORD': '',
'PORT': '',
'USER':''
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,
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.
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.
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.
For any one who has a problem with airflow linked to this issue.
In my case, I’ve initialized airflow in /root/airflow
and run its scheduler as root. I used the run_as_user
parameter to impersonate the web user while running task instances. However airflow was always failing to trigger my DAG with the following errors in logs:
sqlite3.OperationalError: unable to open database file
...
sqlalchemy.exc.OperationalError: (sqlite3.OperationalError) unable to open database file
I also found once I triggered a DAG manually, a new airflow resource directory was automatically created under /home/web
. I’m not clear about this behavior, but I make it work by removing the entire airflow resources from /root
, reinitializing airflow database under /home/web
and running the scheduler as web under:
[root@host ~]# rm -rf airflow
[web@host ~]$ airflow initdb
[web@host ~]$ airflow scheduler -D
If you want to try this approach, I may need to backup your data before doing anything.
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
with c.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.
had the same problem but top answer is too long for me so I suggest to open another shell window type
cd #enter
and try again
in my case, i tried creating the sqlite db in /tmp
folder and from all the slashes i missed a single slash
Instead of sqlite:///tmp/mydb.sqlite
-> sqlite:////tmp/mydb.sqlite
…
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?
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).
Make sure you are not editing the settings.py file while trying to run syncdb, you will get the same error!!!
self.connection = Database.connect(**kwargs)
sqlite3.OperationalError: unable to open database file
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.
I faced the same problem on Windows 7. My database name was test
and I got the error:
self.connection = Database.connect(**kwargs)
sqlite3.OperationalError: unable to open database file
I replaced test
with test.db
and and all went smooth.
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:
conn = lite.connect('folder_A/my_database.db')
And you run the code inside the folder_A
or other places that doesn’t have a folder_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 raises sqlite3.OperationalError
.
from os import mkdir
import sqlite3 as lite
try:
conn = lite.connect('folder_A/my_database.db')
except lite.OperationalError:
mkdir('folder_A')
finally:
conn = lite.connect('folder_A/my_database.db')
On unix I got that error when using the ~
shortcut for the user directory.
Changing it to /home/user
resolved the error.
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!
In my case, the solution was to use an absolute path, to find an existing file:
import os.path
filepath = os.path.abspath(filepath)
# Leave this out if the file doesn't exist yet
assert os.path.exists(filepath), "The file doesn't exist"
conn = sqlite3.connect(filepath)
I don’t know why this fix works: the path only contained ASCII characters and no spaces. Still it made the difference.
For reference: Windows 7, Python 3.6.5 (64-bit).
I was not able to reproduce the issue on another machine (also Windows 7, Python 3.6.4 64-bit), so I have no idea why this fix works.
import sqlite3
connection = sqlite3.connect("d:\pythonAPI\data.db")
cursor = connection.cursor()
create_table = "CREATE TABLE users (id int, username text, password text)"
cursor.execute(create_table)
for clearer full path if you didn’t get it clear
This is definitely a permissions issue. If someone is getting this error on linux, make sure you’re running the command with sudo
as the file most likely is owned by root. Hope that helps!
Use the fully classified name of database file
Use- /home/ankit/Desktop/DS/Week-7-MachineLearning/Week-7-MachineLearning/soccer/database.sqlite
instead-
1) Verify your database path,
check in your settings.py
DATABASES = {
'default': {
'CONN_MAX_AGE': 0,
'ENGINE': 'django.db.backends.sqlite3',
'HOST': 'localhost',
'NAME': os.path.join(BASE_DIR, 'project.db'),
'PASSWORD': '',
'PORT': '',
'USER':''
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,
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.
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.
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.
For any one who has a problem with airflow linked to this issue.
In my case, I’ve initialized airflow in /root/airflow
and run its scheduler as root. I used the run_as_user
parameter to impersonate the web user while running task instances. However airflow was always failing to trigger my DAG with the following errors in logs:
sqlite3.OperationalError: unable to open database file
...
sqlalchemy.exc.OperationalError: (sqlite3.OperationalError) unable to open database file
I also found once I triggered a DAG manually, a new airflow resource directory was automatically created under /home/web
. I’m not clear about this behavior, but I make it work by removing the entire airflow resources from /root
, reinitializing airflow database under /home/web
and running the scheduler as web under:
[root@host ~]# rm -rf airflow
[web@host ~]$ airflow initdb
[web@host ~]$ airflow scheduler -D
If you want to try this approach, I may need to backup your data before doing anything.
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
with c.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.
had the same problem but top answer is too long for me so I suggest to open another shell window type
cd #enter
and try again
in my case, i tried creating the sqlite db in /tmp
folder and from all the slashes i missed a single slash
Instead of sqlite:///tmp/mydb.sqlite
-> sqlite:////tmp/mydb.sqlite
…