views:

120

answers:

2

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?

+1  A: 

This can happen when the owner of the SQLite file itself is not the same as the user running the script. Similar errors can occur if the entire directory path (meaning each directory along the way) can't be written to.

Who owns the SQLite file? You?

Who is the script running as? Apache or Nobody?

Charles
I own the SQLite file, but I don't know who the script runs as. How can I find out? (Keep in mind, this is on a shared host and I have limited permissions)
Austin Hyde
Ah, that makes things more fun. If you're on shared hosting, there's a very good chance that the script runs as "nobody" or "apache". Have your script create a file (`file_put_contents('./foo.txt', 'Hello, world');`), that will show you who it's running as. Chances are that you'll need to have the script create the SQLite database. This may be an entertaining exercise if you already have data in your current file...
Charles
Good idea, but no-go. Whoever PHP is running as doesn't have write privileges, so it can't create the file. Is there anyway PHP can retrieve what user it is currently running as?
Austin Hyde
The only way seems to be [through the POSIX extension](http://us.php.net/manual/en/function.posix-getuid.php), which is enabled by default on POSIX-y systems. Your hosting provider may have [R'd TFM](http://us.php.net/manual/en/intro.posix.php) and disabled it, though.
Charles
Well, they R'd TFM, alright. `posix_getuid()` doesn't work either.
Austin Hyde
Hm. Can you shell out and run `ps aux`? The user Apache is running as (blindly assuming they're running Apache) is almost certainly going to be the user running PHP....
Charles
... that being said, it's a moot point. You aren't root, so you won't be able to change ownership of the SQLite file. Finding out why PHP can't create new files is probably the next step. Try creating a directory with (eww) 0777 permissions and try to see if a PHP script can create a file within. If it can't, you may well be up a creek without a paddle. If it can, try having PHP create a new SQLite file in that directory.
Charles
+1  A: 

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.

Austin Hyde