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 t
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.
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
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.
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.
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.
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,