SQLite basics

Using SQLite

  • To open a SQLite database.
sqlite3 /path/to/database/file
  • Show the tables in a database
.tables
  • To Exit SQLite
.quit

PHP & SQLite

Connect to the database

try{
$dbHandle = new PDO('sqlite:/var/www/lighttpd/noc/emailserver_stat.sqlite');
}catch( PDOException $exception ){
echo "Can NOT connect to database";
die($exception->getMessage());
}

Create a table if it doesn’t exist

$sqlCreateTable = 'CREATE TABLE status (date date NOT NULL default \'0000-00-00\', item varchar(255) NOT NULL, value varchar(255) NOT NULL)';
$dbHandle->exec($sqlCreateTable);

List the conent of a table

$search_date = "2009-02-11";
$sqlGetView = 'SELECT * FROM status WHERE date = "'.$search_date.'"';
$result = $dbHandle->query($sqlGetView);
echo "<table border='1'>";
 while ($entry = $result->fetch()) {
    echo "<tr><td> " . $entry['value'] . "</td><td>" . $entry['item']. "</td></tr>";
 } echo "</table>";

Display a single value from a table

$search_date = "2009-02-11";
$sqlGetView = 'SELECT value FROM status WHERE item = \'connections\' AND date = "'.$search_date.'"';
$result = $dbHandle->query($sqlGetView);
$pageView = $result->fetch();
$connections = $pageView['value'];echo "$search_date
";
echo 'Number Of Connections: '.$connections.'
';
Advertisements

Leave a Reply

Fill in your details below or click an icon to log in:

WordPress.com Logo

You are commenting using your WordPress.com account. Log Out /  Change )

Google photo

You are commenting using your Google account. Log Out /  Change )

Twitter picture

You are commenting using your Twitter account. Log Out /  Change )

Facebook photo

You are commenting using your Facebook account. Log Out /  Change )

Connecting to %s