SQLSTATE[HY000]: General error: 8 attempt to write a readonly database
On the surface of it – especially as SQLite is a single disk file a database contained in a single disk file – this seemed like a simple enough issue to solve. The database file’s write bit for the web server user or group wasn’t set. So, after setting it appropriately, the application would work.
Sure enough, on reviewing the file’s permissions, the write bit wasn’t set for either the user nor the group of the file, similar to the example below.
-r--r--r-- 1 webroot webroot 16K May 10 12:35 data/db.sqlite3
On seeing this, I used chmod to set the write bit for both user and group and reloaded the application, expecting to not see the error again. Strangely, the error persisted. Then, just for a laugh, I set the read, write, and execute bits for all users, and reloaded the application. Still, the error persisted.
It just seemed strange that, despite all relevant bits being set on the file, it was still reporting as read-only. So, I reverted the previous update to the file’s permissions and started doing some hunting around.
If you receive an error while trying to write to a sqlite database (update, delete, drop):
Warning: PDO::query() [function.query]: SQLSTATE[HY000]: General error: 1 unable to open databaseThe folder that houses the database file must be writeable.
My working knowledge of Linux filesystems is a little rusty, so this seemed strange, but I thought I’d test it all the same. Sure enough, after setting the write bit for the webserver user and group on the enclosing directory and the database file, the error disappeared.
So, if you’ve encountered this issue, remember this, and all will be well.