views:

277

answers:

2

Trying to get a simple PHP/Zend Framework setup to create a SQLite databse and manipulate it.

<?php
require_once("Zend/Db.php"); // Zend framework
$db = Zend_Db::factory('Pdo_Sqlite', array("dbname" => "./test.sqlite3"));

$sql = "CREATE TABLE IF NOT EXISTS ".$db->quoteIdentifier("configs")." (".$db->quoteIdentifier("name")." TEXT NOT NULL PRIMARY KEY, ".$db->quoteIdentifier("value")." TEXT NOT NULL);";
echo $sql;
$db->query($sql);

The SQL echoes out as "CREATE TABLE IF NOT EXISTS "configs" ("name" TEXT NOT NULL PRIMARY KEY, "value" TEXT NOT NULL);", which looks right.

But I get a 'Zend_Db_Statement_Exception' with message 'SQLSTATE[HY000]: General error: 14 unable to open database file'. I've tried taking off the leading "./" on the "dbname" variable, and ensured that the folder the PHP file is in has write permissions for everyone. I even tried creating the file with "touch test.sqlite3" and ensured it was writable by everyone.

This is using PHP v5.2.10

A: 

Make sure the path and filename of the SQLite db file is correct. Try to use an absolute path. Also, make sure file and directory are readable by your webserver. Maybe try to

var_dump(is_readable('./test.sqlite3'));

to ensure it really is in the correct directory.

Gordon
+1  A: 

your code is working here (on windows). so the array("dbname" => "./test.sqlite3") syntax is not the problem. as others, i assume it's a permission issue.

some ideas:

  • if you are running the program with php as apache module: does it work if run from the command line? what are the permissions of the file created? adjust the permissions to the apache user accordingly.
  • if above does not work: strace your script (strace php test.php) and check the output for permission errors.
  • the directory where ./test.sqlite3 is to be created must be readable and writable by the user running php. it is not enough to have a readable and writable test.sqlite3, because sqlite also reads and writes a temporary test.sqlite3-journal file in this directory. make sure it is.
  • try using an absolute path for the database name.
  • make sure the pdo and pdo_sqlite extensions are loaded (check the output of phpinfo()). also, see that are no conflicts between the system and the php bundled sqlite libs.
ax
Thanks Ax, I didn't realize SQLite needed a temporary journal file as well. Turns out I hadn't properly set the permissions on the folder to enable everyone to write to it, and once I fixed that, everything was great!
MidnightLightning