Just to point out that "CREATE TABLE IF NOT EXISTS" is only supported in SQLite version 3.3.0 or above. And PHP (currently 5.2.5) only comes with SQLite version 2.1.
Executing a create table like this will throw an error as will creating a table that already exists. Instead execute a normal "CREATE TABLE" command and catch it with "try {..} catch".
sqlite_exec
SQLiteDatabase->exec
(PHP 5, PECL sqlite:1.0.3)
sqlite_exec -- SQLiteDatabase->exec — 与えられたデータベースに対して結果を伴わないクエリを実行する
説明
オブジェクト指向言語型スタイル (メソッド):
与えられたデータベースハンドル (dbhandle パラメータで指定される) に対して query によって指定される SQL ステートメントを実行します。
SQLiteは、セミコロンで区切られた複数のクエリを実行します。 これにより、ファイルからロードするかスクリプトに埋め込んだ SQL をバッチ実行することができます。
パラメータ
- dbhandle
-
SQLite データベースリソース。手続きに従って、 sqlite_open() から返されます。 このパラメータは、 オブジェクト指向言語型メソッドを使用する場合は不要です。
- query
-
実行するクエリを指定します。
- error_msg
-
エラーが発生した場合、指定された変数に詰め込まれます。 SQL 文法エラーは sqlite_last_error() 関数では取得できないので、これは特に重要です。
注意: (MySQL のような)他のデータベースエクステンションとの互換性のため、 2 種類の構文がサポートされています。 推奨されるのは最初の構文で、dbhandle パラメータを 関数の最初のパラメータとするものです。
返り値
この関数はブール型の結果を返します。 成功時は TRUE、失敗時は FALSE を返します。 もしレコードを返すクエリを実行する必要がある場合は sqlite_query() を参照ください。
SQLITE_ASSOC および SQLITE_BOTH で 返されるカラム名は、設定オプション sqlite.assoc_case の値に基づき、 大文字小文字が変換されます。
変更履歴
| バージョン | 説明 |
|---|---|
| 5.1.0 | error_msg パラメータが追加されました。 |
例
例1 手続き型言語スタイルでの例
<?php
$dbhandle = sqlite_open('mysqlitedb');
$query = sqlite_exec($dbhandle, "UPDATE users SET email='jDoe@example.com' WHERE username='jDoe'", $error);
if (!$query) {
exit("Error in query: '$error'");
} else {
echo 'Number of rows modified: ', sqlite_changes($dbhandle);
}
?>
例2 オブジェクト指向言語型スタイルでの例
<?php
$dbhandle = new SQLiteDatabase('mysqlitedb');
$query = $dbhandle->queryExec("UPDATE users SET email='jDoe@example.com' WHERE username='jDoe'", $error);
if (!$query) {
exit("Error in query: '$error'");
} else {
echo 'Number of rows modified: ', $dbhandle->changes();
}
?>
sqlite_exec
17-Apr-2008 09:01
24-Jun-2007 01:33
If you run a multiline SQL command (an INSERT, for example), and there is a SQL error in any of the lines, this function will recognize the error and return FALSE. However, any correct commands before the one with the error will still execute. Additionally, if you run changes() after such an incident, it will report that 0 rows have been changed, even though there were rows added to the table by the successful commands.
An example would be:
<?php
// create new database (OO interface)
$dbo = new SQLiteDatabase("db/database.sqlite");
// create table foo
$dbo->query("CREATE TABLE foo(id INTEGER PRIMARY KEY, name CHAR(255));");
// insert sample data
$ins_query = "INSERT INTO foo (name) VALUES ('Ilia1');
INSERT INTO foo (name) VALUES('Ilia2');
INSECT INTO foo (name) VALUES('Ilia3');";
$dbo->queryExec($ins_query);
// get number of rows changed
$changes = $dbo->changes();
echo "<br />Rows changed: $changes<br />";
// Get and show inputted data
$tableArray = $dbo->arrayQuery("SELECT * FROM foo;");
echo "Table Contents\n";
echo "<pre>\n";
print_r($tableArray);
echo "\n</pre>";
?>
The above code should show that 0 rows have been changed, but that there is new data in the table.
