tags:

views:

157

answers:

3

I've got an SQLite3 file which I'd like to parse using PHP, but I'd rather not have to go to the trouble of loading it into a database which I'd first have to install on my server. Is there any library or function in PHP which allows for the direct parsing of SQLite3 from text or a file?

+6  A: 

SQLite is an embedded database engine, so there's nothing you need to install except the sqlite3 extension.

Opening the database is as simple as:

<?php
$db = new SQLite3('my_database_file.db');
Ben James
The SQLite extension is enabled by default as of PHP 5. Before that time the SQLite library is needed.As taken from php.net
tDo
With PHP 5 to 5.2 the native SQLite extension can only open SQLite version 2 databases. To work with version 3 you have to use pdo_sqlite.This limitation no longer applies to PHP 5.3
djn
A: 

I honestly doubt that the "effort" to install SQLite is comparable to "parsing the file" regardless of the language used.

I suggest you install it and look if a client like Squirrel isn't appropriate for your needs. At worst it will take you a whole 10 minutes to install both, and if it is not what you are looking for you have wasted... 10 minutes.

p.marino
A: 

I prefer the procedural approach when using sqlite library. Here's an example:

<?php 
// create new database (procedural interface) 
$db = sqlite_open("database.db"); 

// create a new database
sqlite_query($db , "CREATE TABLE foo (id INTEGER PRIMARY KEY, name CHAR(50))"); 

// insert sample data 
sqlite_query($db, "INSERT INTO foo (name) VALUES ('name1')"); 
sqlite_query($db, "INSERT INTO foo (name) VALUES ('name2')"); 
sqlite_query($db, "INSERT INTO foo (name) VALUES ('name3')"); 

// execute query 
$result = sqlite_query($db, "SELECT * FROM foo"); 
// iterate through the retrieved rows 
while ($row = sqlite_fetch_array($result)) { 
    print_r($row); 
} 

// close database connection 
sqlite_close($db); 

?>
Yada