FUDforum
Fast Uncompromising Discussions. FUDforum will get your users talking.

Home » Imported messages » comp.lang.php » sqlite and php
Show: Today's Messages :: Polls :: Message Navigator
Switch to threaded view of this topic Create a new topic Submit Reply
sqlite and php [message #175880] Thu, 03 November 2011 20:30 Go to next message
Anne Achey is currently offline  Anne Achey
Messages: 1
Registered: November 2011
Karma: 0
Junior Member
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 22:49 Go to previous messageGo to next message
Tim Streater is currently offline  Tim Streater
Messages: 328
Registered: September 2010
Karma: 0
Senior Member
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 #175884 is a reply to message #175882] Thu, 03 November 2011 22:53 Go to previous messageGo to next message
Jerry Stuckle is currently offline  Jerry Stuckle
Messages: 2598
Registered: September 2010
Karma: 0
Senior Member
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.

--
==================
Remove the "x" from my email address
Jerry Stuckle
JDS Computer Training Corp.
jstucklex(at)attglobal(dot)net
==================
Re: sqlite and php [message #175885 is a reply to message #175880] Thu, 03 November 2011 22:57 Go to previous messageGo to next message
Jerry Stuckle is currently offline  Jerry Stuckle
Messages: 2598
Registered: September 2010
Karma: 0
Senior Member
On 11/3/2011 4:30 PM, Anne Achey 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);
> }
>
> 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

It should work. What version of PHP are you using, and what version of
the sqlite browser are you using?

Your problem could easily be something as simple as a back level browser
not compatible with a newer version of SQLite (or a lot of other things).

Additionally, depending on the OS you're using, does the Sqlite browser
have permission to access the file created by PHP? Often times,
especially in Linux/Unix, the browser has different privileges than the
CLI user.

--
==================
Remove the "x" from my email address
Jerry Stuckle
JDS Computer Training Corp.
jstucklex(at)attglobal(dot)net
==================
Re: sqlite and php [message #175886 is a reply to message #175880] Thu, 03 November 2011 23:14 Go to previous messageGo to next message
P E Schoen is currently offline  P E Schoen
Messages: 86
Registered: January 2011
Karma: 0
Member
"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 23:20 Go to previous messageGo to next message
Tim Streater is currently offline  Tim Streater
Messages: 328
Registered: September 2010
Karma: 0
Senior Member
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 #175888 is a reply to message #175886] Thu, 03 November 2011 23:24 Go to previous messageGo to next message
P E Schoen is currently offline  P E Schoen
Messages: 86
Registered: January 2011
Karma: 0
Member
There's a new version of the browser:

http://sqlitedbrowser.sourceforge.net/

Paul
Re: sqlite and php [message #175889 is a reply to message #175880] Fri, 04 November 2011 00:20 Go to previous messageGo to next message
Denis McMahon is currently offline  Denis McMahon
Messages: 634
Registered: September 2010
Karma: 0
Senior Member
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] Fri, 04 November 2011 02:38 Go to previous message
Denis McMahon is currently offline  Denis McMahon
Messages: 634
Registered: September 2010
Karma: 0
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
  Switch to threaded view of this topic Create a new topic Submit Reply
Previous Topic: email sent from webpage is classed as junk
Next Topic: session cookie: client side
Goto Forum:
  

-=] Back to Top [=-
[ Syndicate this forum (XML) ] [ RSS ]

Current Time: Thu Nov 28 20:09:26 GMT 2024

Total time taken to generate the page: 0.02363 seconds