note 82875 deleted from book.sqlite by cmb
| From: | cmb@php.net | Date: | Fri, 02 Sep 2016 14:09:22 +0000 |
| Subject: | note 82875 deleted from book.sqlite by cmb | ||
| References: | 1 | Groups: | php.notes |
| Request: | Send a blank email to php-notes+get-206467@lists.php.net to get a copy of this message | ||
Note Submitter: saivert at saivert dot com
----
How to open a database, create a table if it doesn't exist and inserting initial value.
<?php
if ($db = new SQLiteDatabase('filename')) {
$q = @$db->query('SELECT requests FROM tablename WHERE id = 1');
if ($q === false) {
$db->queryExec('CREATE TABLE tablename (id int, requests int, PRIMARY KEY (id)); INSERT
INTO tablename VALUES (1,1)');
$hits = 1;
} else {
$result = $q->fetchSingle();
$hits = $result+1;
}
$db->queryExec("UPDATE tablename SET requests = '$hits' WHERE id = 1");
} else {
die($err);
}
?>
Use this as boilerplate code for any new project using SQLite.