Re: sqlite

[Date Prev][Date Next][Thread Prev][Thread Next][Date Index][Thread Index]

 



David McGlone <david@xxxxxxxxxxxxx> wrote:
> places.sqlite is mozilla's bookmarks db and I was trying to read this
> db, but so far I've been unsuccessful.
> 
> sqlite ver: 2.8.17
> PHP version: PHP 5.3.3-1ubuntu9.6 with Suhosin-Patch
> php5-sqlite: 5.3.3-1ubuntu9.6

Okay, first off, mozilla uses sqlite3, not sqlite2, so you need to use
those functions/classes instead.

> try
> {
>   //create or open the database
>   $database = new SQLiteDatabase('places.sqlite', 0666, $error);
> }
> catch(Exception $e)
> {
>   die($error);
> }
> 
> $query = "SELECT * FROM moz_bookmarks";
> 
> if($result = $database->query($query, SQLITE_BOTH, $error))
> {
>   while($row = $result->fetch())
>   {
>     echo ("ID: {$row['id']} <br />" );
>   }
> }
> else
> {
>   die($error);
> }
> 

Using SQLite3, this works, dumping the first record:

<?php
$db = new SQLite3('places.sqlite');
$result = $db->query("select * from moz_bookmarks");
var_dump($result->fetchArray());

> But if I use this code:
> 
> try 
> {
>     /*** connect to SQLite database ***/
> 
>     $dbh = new PDO("sqlite:places.sqlite");
>     echo "Handle has been created ...... <br><br>";
>     
> 
> }
> catch(PDOException $e)
> {
>     echo $e->getMessage();
>     echo "<br><br>Database -- NOT -- loaded successfully .. ";
>     die( "<br><br>Query Closed !!! $error");
> }
> 
> echo "Database loaded successfully ....";
> 
> I get the expected output, but can't figure out how to change the above
> script to echo the contents of the DB. I just get the two messages and
> that's it.

This works as it does because PDO uses SQLite3.

Read http://us.php.net/manual/en/book.pdo.php thoroughly.

To submit a query using PDO, it's just:

$result = $dbh->query('select * from moz_bookmarks');

which returns an object of PDOStatement class in $result. Then you can
just loop on $result->fetch() to deal with each row.

This is a PDO version of the above:

try {
  $db = new PDO('sqlite:places.sqlite');
}
catch (PDOException $e) {
  die("SQLite connection failed ".$e->getMessage());
}

if ($result = $db->query("select * from moz_bookmarks")) {
  var_dump($result->fetch());
} else {
  die("Query failed.");
}

-- 
PHP Database Mailing List (http://www.php.net/)
To unsubscribe, visit: http://www.php.net/unsub.php



[Index of Archives]     [PHP Home]     [PHP Users]     [Postgresql Discussion]     [Kernel Newbies]     [Postgresql]     [Yosemite News]

  Powered by Linux