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 :)
SQLite
- Giriş
- Yapılandırma/Kurulum
- Öntanımlı Sabitler
- SQLite İşlevleri
- sqlite_array_query — Belirtilen veritabanı üzerinde bir sorgu çalıştırıp sonucu bir dizi içinde döndürür
- sqlite_busy_timeout — Meşgul bekleme zaman aşımını belirler
- sqlite_changes — En son SQL deyiminin değiştirdiği satır sayısını döndürür
- sqlite_close — Açık bir SQLite veritabanını kapatır
- sqlite_column — Sonuç kümesindeki geçerli satırdan belirtilen sütunu döndürür
- sqlite_create_aggregate — SQL deyimlerinde kullanmak üzere toparlayıcı bir kullanıcı tanımlı işlevi kayda geçirir
- sqlite_create_function — SQL deyimlerinde kullanmak üzere bir kullanıcı tanımlı işlevi kayda geçirir
- sqlite_current — Sonuç kümesindeki geçerli satırı bir dizi içinde döndürür
- sqlite_error_string — Bir hata kodu ile ilgili açıklamayı döndürür
- sqlite_escape_string — Bir sorgu değiştirgesi olarak kullanmak üzere bir dizgeyi önceler
- sqlite_exec — Belirtilen veritabanı üzerinde sonuç döndürmeyen bir sorgu çalıştırır
- sqlite_factory — Bir SQLite veritabanı açıp bir SQLiteDatabase nesnesi döndürür
- sqlite_fetch_all — Sonuç kümesindeki tüm satırları bir satır dizileri dizisi olarak döndürür
- sqlite_fetch_array — Sonuç kümesindeki sonraki satırı bir dizi içinde döndürür
- sqlite_fetch_column_types — Belli bir tablodaki sütun türlerini bir dizi içinde döndürür
- sqlite_fetch_object — Sonuç kümesindeki sonraki satırı bir nesne olarak döndürür
- sqlite_fetch_single — Sonuç kümesindeki ilk sütunu bir dizge olarak döndürür
- sqlite_fetch_string — sqlite_fetch_single işlevinin takma adıdır
- sqlite_field_name — Sonuç kümesindeki belli bir alanın ismini döndürür
- sqlite_has_more — Sonuç kümesinde başka satır kaldı mı diye bakar
- sqlite_has_prev — Geçerli satırın öncesinde satır var mı diye bakar
- sqlite_key — Geçerli satırın indisini döndürür
- sqlite_last_error — Veritabanındaki son hatanın kodunu döndürür
- sqlite_last_insert_rowid — Son yerleştirilen satırın satır kimliğini döndürür
- sqlite_libencoding — İlintili SQLite kütüphanesinin karakter kodlamasını döndürür
- sqlite_libversion — İlintili SQLite kütüphanesinin sürüm numarasını döndürür
- sqlite_next — Sonraki satıra gider
- sqlite_num_fields — Sonuç kümesindeki alan sayısını döndürür
- sqlite_num_rows — Tamponlu bir sonuç kümesindeki satır sayısını döndürür
- sqlite_open — Bir SQLite veritabanı için bir tanıtıcı açar, veritabanı mevcut değilse oluşturur
- sqlite_popen — Bir SQLite veritabanı için kalıcı bir tanıtıcı açar, veritabanı mevcut değilse oluşturur
- sqlite_prev — Sonuç kümesindeki önceki satıra gider
- sqlite_query — Belirtilen veritabanında bir sorgu çalıştırıp bir sonuç tanıtıcısı döndürür
- sqlite_rewind — İlk satırı geçerli satır yapar
- sqlite_seek — Tamponlu bir sonuç kümesinde belli bir satır numarasına gider
- sqlite_single_query — Bir sorgu çalıştırıp sonucu tek bir sütunluk veya tek bir satırlık bir dizi olarak döndürür
- sqlite_udf_decode_binary — Kullanıcı tanımlı bir işleve değiştirge olarak aktarılacak ikil veriyi çözümler
- sqlite_udf_encode_binary — Bir kullanıcı tanımlı işlevden dönecek ikil veriyi döndürmeden önce kodlamak için kullanılır
- sqlite_unbuffered_query — Sonuçların alınıp bir tampona konmadığı bir sorgu çalıştırır
- sqlite_valid — Başka satır var mı diye bakar
nosdudefr at gmail dot com
06-May-2011 06:34
Anonymous
24-Jul-2010 07:12
As of July 2010, there are two ways to use SQLite from PHP:
- procedural: sqlite (=sqlite2), sqlite3
- object-oriented: SQLite3, PDO
Andrew Paul Dickey
12-Jun-2009 12:43
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.
saivert at saivert dot com
30-Apr-2008 03:02
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.
