If you’re encountering “General error - 8 attempt to write a readonly database” when working with SQLite, and confused because the write permission’s set on the file, here’s the solution.
:idseparator: -
:idprefix:
:experimental:
:source-highlighter: rouge
:rouge-style: pastie
:imagesdir: /images
Earlier this week, after deploying a link:/tags/mezzio/[Mezzio] application, one that interacts with https://www.sqlite.org/index.html[SQLite], I encountered the following error:
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 https://www.sqlite.org/onefile.html[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 https://linux.die.net/man/1/chmod[chmod] to set https://linuxize.com/post/understanding-linux-file-permissions/[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.
Low and behold, thanks to https://stackoverflow.com/questions/3319112/sqlite-error-attempt-to-write-a-readonly-database-during-insert[a post on Stack Overflow], I found the issue, one which I never expected.
Buried in the comments of the PHP manual’s entry for the PDO SQLite driver https://www.php.net/manual/en/ref.pdo-sqlite.php#57356[is the following]:
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 database
The 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.
Join the discussion
comments powered by Disqus