As of July 2010, there are two ways to use SQLite from PHP:
- procedural: sqlite (=sqlite2), sqlite3
- object-oriented: SQLite3, PDO
SQLite
- 導入
- インストール/設定
- 定義済の定数
- SQLite 関数
- sqlite_array_query — 指定したデータベースに対してクエリを実行し、配列を返す
- sqlite_busy_timeout — ビジータイムアウト時間を設定またはビジーハンドラを無効にする
- sqlite_changes — 直近のSQLステートメントにより変更されたレコード数を返す
- sqlite_close — オープンされたSQLiteデータベースを閉じる
- sqlite_column — カレントの結果セットのレコードからカラムを1列取得する
- sqlite_create_aggregate — SQLステートメントで使用する集約UDFを登録する
- sqlite_create_function — SQLステートメントで使用するために"通常の"ユーザー定義関数を登録する
- sqlite_current — 結果セットからカレントのレコードを配列として取得する
- sqlite_error_string — エラーコードの説明を返す
- sqlite_escape_string — クエリパラメータ用に文字列をエスケープする
- sqlite_exec — 与えられたデータベースに対して結果を伴わないクエリを実行する
- sqlite_factory — SQLite データベースをオープンし、SQLiteDatabse オブジェクトを返す
- sqlite_fetch_all — 結果セットから全てのレコードを配列の配列として取得する
- sqlite_fetch_array — 結果セットから次のレコードを配列として取得する
- sqlite_fetch_column_types — 特定のテーブルからカラム型の配列を返す
- sqlite_fetch_object — 結果セットから次のレコードをオブジェクトとして取得する
- sqlite_fetch_single — 結果セットの最初のカラムを文字列として取得する
- sqlite_fetch_string — sqlite_fetch_single のエイリアス
- sqlite_field_name — 特定のフィールドの名前を返す
- sqlite_has_more — まだレコードがあるかないかを返す
- sqlite_has_prev — 前のレコードがあるかどうかを返す
- sqlite_key — カレントレコードのインデックスを返す
- sqlite_last_error — データベースに関する直近のエラーコードを返す
- sqlite_last_insert_rowid — 直近に挿入されたレコードのrowidを返す
- sqlite_libencoding — リンクされているSQLiteライブラリのエンコーディングを返す
- sqlite_libversion — リンクされているSQLiteライブラリのバージョンを返す
- sqlite_next — 次のレコード番号へシークする
- sqlite_num_fields — 結果セットのフィールド数を返す
- sqlite_num_rows — 結果セットのレコード数を返す
- sqlite_open — SQLiteデータベースをオープンする。データベースが存在しない場合は作 成する
- sqlite_popen — SQLiteデータベースへの持続的ハンドルをオープンする。存在しない場合 には、データベースを作成する
- sqlite_prev — 結果セットの前のレコード番号へシークする
- sqlite_query — 指定したデータベースに対してクエリを実行し、結果ハンドル を返す
- sqlite_rewind — 先頭レコード番号へシークする
- sqlite_seek — 特定のレコード番号へシークする
- sqlite_single_query — クエリを実行し、単一カラムもしくは先頭レコードの値に対する配列を返す
- sqlite_udf_decode_binary — UDFにパラメータとして渡されたバイナリデータをデコードする
- sqlite_udf_encode_binary — UDFから返す前にバイナリデータをエンコードする
- sqlite_unbuffered_query — 事前取得していないクエリを実行し、全てのデータをバッファリングする
- sqlite_valid — まだレコードが残っているかどうかを返す
Anonymous ¶
2 years ago
nosdudefr at gmail dot com ¶
2 years ago
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 :)
Andrew Paul Dickey ¶
3 years ago
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 ¶
5 years ago
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.
