©
本文檔使用 php中文網手册 發布
[#1] nosdudefr at gmail dot com [2011-05-06 03:34:07]
Regarding the table creation you can optimize this code a bit by using the built in " CREATE TABLE IF NOT EXISTS <tablename>". This will let the table creation decision to the sqlite engine.
Then, if i may, your code could be something like :
<?php
if ($db = new SQLiteDatabase('filename')) {
// first let the engine check table, and create it eventualy
$q = @$db->query('CREATE TABLE IF NOT EXISTS tablename (id int, requests int, PRIMARY KEY (id))';
//the rest of the code, according error checks etc
// ...
?>
For more "tweaks" feel free to look at sqlite language ref : http://www.sqlite.org/lang_createtable.html
Have fun with this powerfull&simple engine :)
[#2] Anonymous [2010-07-24 16:12:38]
As of July 2010, there are two ways to use SQLite from PHP:
- procedural: sqlite (=sqlite2), sqlite3
- object-oriented: SQLite3, PDO
[#3] Andrew Paul Dickey [2009-06-11 21:43:48]
If you intend to implement 2.x releases of SQLite with your development you are in the right place, as this library is suitable for use with your application (reference http://us.php.net/manual/en/book.sqlite.php).
If you intend to use SQLite 3.x releases of SQLite with your development please refer to the section on PHP Data Objects, and specifically the PDO-SQLite implementation available at(references: http://us.php.net/manual/en/book.pdo.php , http://au2.php.net/manual/en/ref.pdo-sqlite.php).
It is my hope that this post will save both new users and experienced developers time during their initial or a new implementation of PHP & SQLite by encouraging them to use the appropriate libraries.
[#4] saivert at saivert dot com [2008-04-30 00:02:30]
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.