sqlite and php [message #175880] |
Thu, 03 November 2011 16:30 |
Anne Achey
Messages: 1 Registered: November 2011
Karma: 0
|
Junior Member |
add to buddy list ignore all messages by this user
|
|
Hi,
Anyone know how to create a sqlite database from PHP code? I am trying
the code below:
try
{
//create or open the database
$database = new SQLiteDatabase('myDatabase.db', 0666, $error);
}
catch(Exception $e)
{
die($error);
}
But when I try to access this database with the sqlite browser, it
tthrows an error saying the "file is not a SQL3 Database"
Error at the following link
http://imageshack.us/photo/my-images/259/errortz.png/
I then created the database manually from the sqlite command line and
tried to access it with PDO in PHP. I was able to do this, but again I
also want to be able to CREATE A DATABASE be it with PDO or
SQLiteDatabase.
Any help will be appreciated!
AA
|
|
|
Re: sqlite and php [message #175882 is a reply to message #175880] |
Thu, 03 November 2011 18:49 |
Tim Streater
Messages: 328 Registered: September 2010
Karma: 0
|
Senior Member |
add to buddy list ignore all messages by this user
|
|
In article
<-76e7-4b70-b81b-67d5b8b8522a(at)q13g2000vbd(dot)googlegroups(dot)com>,
Anne Achey <anneachey(at)gmail(dot)com> wrote:
> Hi,
>
> Anyone know how to create a sqlite database from PHP code? I am trying
> the code below:
>
> try
> {
> //create or open the database
> $database = new SQLiteDatabase('myDatabase.db', 0666, $error);
> }
> catch(Exception $e)
> {
> die($error);
> }
What's all this rubbish?
Try:
$connstr = "sqlite:" . $name_of_my_db;
$dbh = new PDO ($connstr);
$dbh->setAttribute (PDO::ATTR_ERRMODE, PDO::ERRMODE_SILENT);
and then to use it:
$res = $dbh->query ($sql);
if ($res===false)
{
$earray = $dbh->errorInfo ();
// Then $earray[0] has error code, more info in [1] and [2]
}
and so on. Have you not looked at the PHP website docs?
--
Tim
"That excessive bail ought not to be required, nor excessive fines imposed,
nor cruel and unusual punishments inflicted" -- Bill of Rights 1689
|
|
|
|
|
Re: sqlite and php [message #175886 is a reply to message #175880] |
Thu, 03 November 2011 19:14 |
|
"Anne Achey" wrote in message
news:10794cb1-76e7-4b70-b81b-67d5b8b8522a(at)q13g2000vbd(dot)googlegroups(dot)com...
> Anyone know how to create a sqlite database from PHP code?
[snip]
> Any help will be appreciated!
You might try downloading this, which is a single open source PHP file which
performs pretty much all SQLite database functions:
http://code.google.com/p/phpliteadmin/
I also downloaded an SQLite browser with a Windows GUI that is useful for
creating databases:
http://sourceforge.net/projects/sqlitebrowser/
I have modified the phpliteadmin script to remove some features and add
others. It's well documented and actively supported by the developer, and
it's a good way to get a jump start on a useful server application.
Paul
|
|
|
Re: sqlite and php [message #175887 is a reply to message #175884] |
Thu, 03 November 2011 19:20 |
Tim Streater
Messages: 328 Registered: September 2010
Karma: 0
|
Senior Member |
add to buddy list ignore all messages by this user
|
|
In article <j8v61o$i2r$1(at)dont-email(dot)me>,
Jerry Stuckle <jstucklex(at)attglobal(dot)net> wrote:
> On 11/3/2011 6:49 PM, Tim Streater wrote:
>> In article <-76e7-4b70-b81b-67d5b8b8522a(at)q13g2000vbd(dot)googlegroups(dot)com>,
>> Anne Achey <anneachey(at)gmail(dot)com> wrote:
>>
>>> Hi,
>>>
>>> Anyone know how to create a sqlite database from PHP code? I am trying
>>> the code below:
>>>
>>> try
>>> {
>>> //create or open the database
>>> $database = new SQLiteDatabase('myDatabase.db', 0666, $error);
>>> }
>>> catch(Exception $e)
>>> {
>>> die($error);
>>> }
>>
>> What's all this rubbish?
>>
>> Try:
>>
>>
>> $connstr = "sqlite:" . $name_of_my_db;
>> $dbh = new PDO ($connstr);
>>
>> $dbh->setAttribute (PDO::ATTR_ERRMODE, PDO::ERRMODE_SILENT);
>>
>> and then to use it:
>>
>> $res = $dbh->query ($sql);
>>
>> if ($res===false)
>> {
>> $earray = $dbh->errorInfo ();
>> // Then $earray[0] has error code, more info in [1] and [2]
>> }
>>
>> and so on. Have you not looked at the PHP website docs?
>>
>
> A completely different interface, Tim. The op's code should work.
Maybe. But I know nothing of SQLiteDatabase, and the OP said this:
>> I then created the database manually from the sqlite command line and
>> tried to access it with PDO in PHP. I was able to do this, but again I
>> also want to be able to CREATE A DATABASE be it with PDO or
>> SQLiteDatabase.
So as requested, I indicated how to do it with PDO.
--
Tim
"That excessive bail ought not to be required, nor excessive fines imposed,
nor cruel and unusual punishments inflicted" -- Bill of Rights 1689
|
|
|
|
Re: sqlite and php [message #175889 is a reply to message #175880] |
Thu, 03 November 2011 20:20 |
|
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?
Rgds
Denis McMahon
|
|
|
Re: sqlite and php [message #175891 is a reply to message #175889] |
Thu, 03 November 2011 22:38 |
|
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
|
|
|