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条回答
何必那么认真
2楼-- · 2020-01-25 01:57

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.

查看更多
手持菜刀,她持情操
3楼-- · 2020-01-25 02:01
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

查看更多
兄弟一词,经得起流年.
4楼-- · 2020-01-25 02:02

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.

查看更多
狗以群分
5楼-- · 2020-01-25 02:04

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!

查看更多
老娘就宠你
6楼-- · 2020-01-25 02:08

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).

查看更多
登录 后发表回答