Sqlite3, OperationalError: unable to open database

2020-01-25 01:23发布

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?

标签: python sqlite
17条回答
forever°为你锁心
2楼-- · 2020-01-25 01:53

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.

查看更多
smile是对你的礼貌
3楼-- · 2020-01-25 01:53

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.

查看更多
冷血范
4楼-- · 2020-01-25 01:54

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
查看更多
劳资没心,怎么记你
5楼-- · 2020-01-25 01:54

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,

查看更多
劳资没心,怎么记你
6楼-- · 2020-01-25 01:55

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')
查看更多
Evening l夕情丶
7楼-- · 2020-01-25 01:56

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.

查看更多
登录 后发表回答