SQLite error ‘attempt to write a readonly database’ during insert

permissionssqlite

I have a SQLite database that I am using for a website. The problem is that when I try to INSERT INTO it, I get a PDOException

SQLSTATE[HY000]: General error: 8 attempt to write a readonly database

I SSH'd into the server and checked permissions, and the database has the permissions

-rw-rw-r--

I'm not that familiar with *nix permissions, but I'm pretty sure this means

  • Not a directory
  • Owner has read/write permissions (that's me, according to ls -l)
  • Group has read/write permissions
  • Everyone else only has read permissions

I also looked everywhere I knew to using the sqlite3 program, and found nothing relevant.

Because I didn't know with what permissions PDO is trying to open the database, I did

chmod o+w supplies.db

Now, I get another PDOException:

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

But it ONLY occurs when I try to execute an INSERT query after the database is open.

Any ideas on what is going on?

Best Answer

The problem, as it turns out, is that the PDO SQLite driver requires that if you are going to do a write operation (INSERT,UPDATE,DELETE,DROP, etc), then the folder the database resides in must have write permissions, as well as the actual database file.

I found this information in a comment at the very bottom of the PDO SQLite driver manual page.