SQLite error 'attempt to write a readonly database' during insert?

Viewed 199445

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?

11 Answers

In summary, I've fixed the problem by putting the database file (* .db) in a subfolder.

  • The subfolder and the database file within it must be a member of the www-data group.
  • In the www-data group, you must have the right to write to the subfolder and the database file.

####### Additional Notes For Similar Problem #####

I gave write permissions to my sqlite database file to other users and groups but it still didn't work.

File is in my web root directory for my .NET Core WebApi.

It looked like this:

-rw-rw-rw-  1 root root  24576 Jan 28 16:03 librestore.db

Even if I ran the service as root, I kept getting the error :

Error: SQLite Error 8: 'attempt to write a readonly database'.

I also did a chown to www-data on the librestore.db and I still received the same error.

Finally I moved up above my webroot directory and gave others write access to that directory (LibreStore - the root of my WebApi) also and then it worked.

Web root has write access

I'm not sure why I had to give the directory write access if the specific file already had write access, but this is the only thing that worked.

But once I made that change www-data user could access the .db file and inserts succeeded.

I used:

echo exec('whoami');

to find out who is running the script (say username), and then gave the user permissions to the entire application directory, like:

sudo chown -R :username /var/www/html/myapp

(For followers looking for an answer to a similar question) I'm building a C# .Net Core 6.0 WPF app. I put the Sqlite.db3 on the c:\ drive for convenience while developing. To write to the database I must open Visual Studio 2019 as Administrator.

@Charles in a comment pointed out the solution to this (or at least, a botch solution). This is merely me spelling it out more clearly. Put file_put_contents('./nameofyourdb.sqlite', null); (or .db, whichever you fancy) in a .php file in the root directory of your app (or wherever you want the db to be created), then load that page which renders the php code. Now you have an sqlite db created by whichever user runs your php code, meaning your php code can write to it. Just don't forget to use sudo when interacting with this db in the console.

A good clean solution to this is to allow the file of your main user account to be written to by (in my case) the http user but this worked for me and its simple.

Related