Unterstützung für SQLite3-Datenbanken.
In Ubuntu the package php5-sqlite3 uses this method for accessing sqlite3 databases:
(from /usr/share/doc/php5-sqlite3/examples/example1.php)
<?php
/*
* create a SQLite3 handle.
*
* Note: in-memory database are created by the magic keyword ":memory:"
*
*/
$db = sqlite3_open(":memory:");
if (!$db) die ("Could not create in-memory database..");
/*
* create a simple test and insert some values..
*/
$ret = sqlite3_exec ($db, "CREATE TABLE test (id INTEGER, name TEXT, age INTEGER);");
if (!$ret) die (sqlite3_error($db));
sqlite3_exec($db, "INSERT INTO test (id,name,age) VALUES (1,'michael',32)");
sqlite3_exec($db, "INSERT INTO test (id,name,age) VALUES (2,'bob',27)");
sqlite3_exec($db, "INSERT INTO test (id,name,age) VALUES (3,'martin',12)");
/*
* Create a query
*/
$query = sqlite3_query($db, "SELECT * FROM test ORDER BY age DESC");
if (!$query) die (sqlite3_error($db));
/*
* sqlite3_fetch_array() returns an associative array
* for each row in the result set. Key indexes are
* the columns names.
*/
while ( ($row = sqlite3_fetch_array($query)))
{
printf("%-20s %u\n", $row['name'], $row['age']);
}
/*
* do not forget to release all handles !
*/
sqlite3_query_close($query);
sqlite3_close ($db);
?>
If you use PHP5, the currently advised approach to natively (ie, without using a third party library) interfacing with sqlite3 databases is through the use of a PDO object:
<?php
$dbh = new PDO('sqlite:yourdatabase.db');
?>
Any execution of functions and commands supported by sqlite3 can then be called through the PDO query and exec functions. For instance, a broad select would be achieved as follows:
<?php
foreach($dbh->query('SELECT * FROM table WHERE column = criterium') as $row)
{
foreach($row as $key=>$val)
{
// PDO has no built in fetch_assoc equivalent
if(!is_numeric($key)) print "$key: $val\n";
}
}
?>
when done consulting or administrating a database that relies on PDO access, it is generally a good idea to either issue a
<?php $dbh = null; ?>
or
<?php unset($dbh); ?>
statement, as the PDO class does not come with a function for closing a connection, and so any PDO connection will sit idle until you explicitly dispose of it.