Re: sqlite and php [message #175891 is a reply to message #175889] |
Fri, 04 November 2011 02:38 |
Denis McMahon
Messages: 634 Registered: September 2010
Karma:
|
Senior Member |
|
|
On Fri, 04 Nov 2011 00:20:20 +0000, Denis McMahon wrote:
> On Thu, 03 Nov 2011 13:30:11 -0700, Anne Achey wrote:
>
>> Anyone know how to create a sqlite database from PHP code? I am trying
>> the code below:
>
>> $database = new SQLiteDatabase('myDatabase.db', 0666, $error);
>
>> "file is not a SQL3 Database"
>
> Is sqlite the same as sqlite3?
Following up:
<?php
//
// can sqlite open sqlite3 files and vice versa?
//
echo "sql3 create, write, read\n";
$db = new SQLite3('mysqlitedb3.db');
if ($db)
{
$db->exec('CREATE TABLE foo (bar STRING)');
$db->exec("INSERT INTO foo (bar) VALUES ('This is a test')");
$result = $db->query('SELECT bar FROM foo');
if ($result) var_dump($result->fetchArray());
else echo $db->lastErrorMsg()."\n";
}
echo "sql create, write, read\n";
if ($db = sqlite_open('mysqlitedb.db', 0666, $sqliteerror))
{
sqlite_query($db, 'CREATE TABLE foo (bar varchar(10))');
sqlite_query($db, "INSERT INTO foo VALUES ('fnord')");
$result = sqlite_query($db, 'select bar from foo');
var_dump(sqlite_fetch_array($result));
}
else
{
die($sqliteerror);
}
echo "sql read sql3 db\n";
if ($db = sqlite_open('mysqlitedb3.db', 0666, $sqliteerror))
{
$result = sqlite_query($db, 'select bar from foo');
var_dump(sqlite_fetch_array($result));
}
else
{
echo $sqliteerror."\n";
}
echo "sql3 read sql db\n";
$db = new SQLite3('mysqlitedb.db');
if ($db)
{
$result = $db->query('SELECT bar FROM foo');
if ($result) var_dump($result->fetchArray());
else echo $db->lastErrorMsg()."\n";
}
?>
Run from the command line:
=======================================================================
$ php sqlite3sqlite.php
sql3 create, write, read
array(2) {
[0]=>
string(14) "This is a test"
["bar"]=>
string(14) "This is a test"
}
sql create, write, read
array(2) {
[0]=>
string(5) "fnord"
["bar"]=>
string(5) "fnord"
}
sql read sql3 db
PHP Warning: sqlite_open(): file is encrypted or is not a database in /
home/denis/tmp/sqlite3sqlite.php on line 28
file is encrypted or is not a database
sql3 read sql db
PHP Warning: SQLite3::query(): Unable to prepare statement: 26, file is
encrypted or is not a database in /home/denis/tmp/sqlite3sqlite.php on
line 42
file is encrypted or is not a database
$
=======================================================================
So I'm guessing that the problem here may be that the OP is trying to mix
and match sqlite and sqlite3 databases, and the databases aren't having
it.
Rgds
Denis McMahon
|
|
|