SQLite3: Unable to Open Database File

Recently, I’m working on my Skypiea project which is powered by SQLite3 and Python (as CGI script). Yesterday I encountered a program: When the web server ran a CGI script which would update the database, I got an error message said “sqlite3.OperationalError: unable to open database file“.

I searched the web and Google told me I need to specify the full path (no ~ or $HOME etc.) of the database file and the web server needs the write permission to the file. Sure, I actually had done both before the exception occurred.

Having being pissed for a while, I finally figured out what was wrong here: The web server needs the write permission to not only the database file, but also the containing directory of that file.

Yea, just simple like that.

40 thoughts on “SQLite3: Unable to Open Database File

  1. This had me occupied for an entire afternoon. I was convinced it was a concurrency problem that mutated into a file locking/access error until I found your explanation. Thank you so much!

  2. Thanks!! Good your page is the first in google results.

    Actually that makes sense, I guess sqlite creates a temporary file in the directory and rewrites the old file with the temporary on commit.

  3. I am developing an application for a service desk manager and
    it uses a storage platform SqlLite, broke the
    head all afternoon to find very little detail that just taking everyone’s patience.
    Thanks for the help.
    Brazil – SP

  4. thanks.. it was very useful . I was trying to look at this solution for few hours thinking that after modifying the writing permission on the file it was enough.

  5. I wish I could also say Thanks Thanks :)
    But I am trying from C code to open/create the db file:
    The Path is “/data/databases/mp3.db” and I use CREATE Flag to create the file and directory if it doesn’t exists.
    sqlite3_open_v2(Path, .., SQLITE_OPEN_CREATE | SQLITE_OPEN_READWRITE), NULL);

    And it returns error code 14 /* Unable to open the database file */
    Could you please try to help me as well :)
    Thanks

    • It seems you are trying to create directories under root directory. If your code wasn’t executed by root user, it usually doesn’t have permission to do so.

  6. I am an apache newbie. How can I set the server to have wrtie access to teh sqlite database file and the folder ? I have done the below and it still doesnot work.

    sudo chmod -R 777 databasefolder

  7. Pingback: error SQLite3 Unable to Open Database File « Desarrollo Linux y software libre

  8. Thanks so much for this post. I could have been stalled by this for a long time if I hadn’t come across this post. Thanks to you, I only wasted about five minutes debugging this error.

  9. pfuuu !

    i ran in the same probleme, i tought about that, but i have prefered to search before spending days on it ! you help me thanks !

  10. You’ve just saved me. I was going round in circles trying to work this out. Also realised the folder needs delete permissions too for the journal file!

  11. I have wasted hours and hours on this issue. Thanks for your tip, which helped me get past this problem.

  12. Fortunately, your post shows up as one of the first for “unable to open database file 14″. So I only wasted a few minutes. Thanks! :)

  13. Thank you so much!!!!
    You have no idea how helpful it was!
    I could finally figure out why windows service was refusing to write to the sqlite database.
    Thanks again!

  14. Thanks a lot, this helped me. And to add a bit, the whole path does not need to be web server writable, only the directory for the database file and the database file itself.

    Seems to work both for CGI style and mod_python style Python scripts.

Leave a Reply

Your email address will not be published. Required fields are marked *

*

You may use these HTML tags and attributes: <a href="" title=""> <abbr title=""> <acronym title=""> <b> <blockquote cite=""> <cite> <code> <del datetime=""> <em> <i> <q cite=""> <strike> <strong>