Unable to open database file sqlite

November 29, 2014

When writing a web app using an SQLite database file, you might run into an error where you can read from the file but not write to it. Despite the database file being writable to the user as it should be.

In PHP, this looks something like:

SQLSTATE[HY000]: General error: 14 unable to open database file.

sqlite3.OperationalError: unable to open database file

What gives? The error is particularly confusing because you just did open the database file and read from it just fine.

Turns out, the problem is the permissions on the directory the database resides in. When writing to an SQLite file, the database engine need to be able to write a lockfile to the database directory.

The fix is easy. For instance, if your web server runs as the user/group www-data , you’d do something like:

Was this helpful? Buy me a coffee with Bitcoin! (What is this?)

Updating Adobe Flash Without Restarting Firefox

No reason for a Flash upgrade to shut down your entire browser, even if it claims so.It’s 2015, and the love-hate relationship of the Web. … Continue reading

Читайте также:  Монстер саунд орехово зуево

Reddit’s Fail-Alien (or "Fail-ien?")

Distraction free writing in a "big boring system"

Plan your next holiday with a Camper Van Rental. Enjoy!

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:

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:

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?

or some other short form? – chrisaycock Jan 9 ’11 at 0:31

17 Answers 17

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

Comments

Copy link Quote reply

Monisha9255 commented Oct 28, 2017 •

The above exception was the direct cause of the following exception:

After this statement

This comment has been minimized.

Copy link Quote reply

vkosuri commented Oct 29, 2017

@Monisha9255 Usually sqlite db file will created in current directory . Due to lack of permission sqlite unable to create db.sqlite3 file on your current directory.

And also you are trying through pyshell May be sqlite3 doesn’t have permission to create db.sqlite3 Could you please try where you write permission to create files.

This comment has been minimized.

Copy link Quote reply

adamjameshuston commented Mar 24, 2018

@vkosuri @gunthercox I’m also having this problem, but I’ve used chmod and 000-default.conf to open up all the permissions for the app’s directory and nothing has changed.

This comment has been minimized.

Copy link Quote reply

vkosuri commented Mar 25, 2018

I unable to figured to problem could you let me know

  1. chatterbot version
  2. Operatung system
  3. Python or anaconda
  4. Full stack trace

This comment has been minimized.

Copy link Quote reply

adamjameshuston commented Mar 25, 2018

@vkosuri Thanks for looking in!

  1. 0.8.4
  2. Apache/2.4.10 on Raspbian – it’s Debian for Raspberry Pi
  3. Python
  4. (below)

This comment has been minimized.

Copy link Quote reply

adamjameshuston commented Mar 25, 2018

This comment has been minimized.

Copy link Quote reply

vkosuri commented Mar 25, 2018

I believe it an operating system error nkt chatterbot issue.

This link will solve your problem https://severfault.com/a/57597

Читайте также:  Как проверить айфон через айтюнс

This comment has been minimized.

Copy link Quote reply

vkosuri commented Mar 25, 2018

This comment has been minimized.

Copy link Quote reply

adamjameshuston commented Mar 27, 2018

@vkosuri Thanks for the help – those were good links

I think you’re totally right, it’s the operating system

Changing permissions wasn’t the answer, though

@gunthercox Instead of letting chatterbot create a new database, I moved the database it created for itself on my local machine while I was testing it to the app’s current directory on the live server

It was able to access that file and has been humming along happily since!

This comment has been minimized.

Copy link Quote reply

vkosuri commented Mar 27, 2018

Glad to hear your problem resolved, If possible could you make a PR with your suggestions?

This comment has been minimized.

Copy link Quote reply

Abhi215 commented Nov 13, 2018

@adamjameshuston Can you please elaborate how did you move the databse

This comment has been minimized.

Copy link Quote reply

yashshukla183 commented Jan 5, 2019

try with sudo , it worked for me.

This comment has been minimized.

Copy link Quote reply

yashshukla183 commented Jan 5, 2019

@Monisha9255 Usually sqlite db file will created in current directory . Due to lack of permission sqlite unable to create db.sqlite3 file on your current directory.

And also you are trying through pyshell May be sqlite3 doesn’t have permission to create db.sqlite3 Could you please try where you write permission to create files.

try to run using sudo, it will work

This comment has been minimized.

Copy link Quote reply

hemdai commented Mar 15, 2019 •

  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,

This comment has been minimized.

Copy link Quote reply

lock bot commented Jan 9, 2020

This thread has been automatically locked since there has not been any recent activity after it was closed. Please open a new issue for related bugs.

Добавить комментарий

Ваш адрес email не будет опубликован. Обязательные поля помечены *

Adblock detector