Note that the SQLITE3_OPEN_READONLY flag cannot be combined with the SQLITE3_OPEN_CREATE flag. If you combine both of these flags, a rather unhelpful "Unable to open database: out of memory" exception will be thrown.(PHP 5 >= 5.3.0, PHP 7, PHP 8)
SQLite3::__construct — SQLite3 オブジェクトを作成し、SQLite 3 データベースをオープンする
$filename, int $flags = SQLITE3_OPEN_READWRITE | SQLITE3_OPEN_CREATE, string $encryptionKey = "")SQLite3 オブジェクトを作成し、SQLite 3 データベースをオープンします。 暗号化込みでビルドされている場合は、キーの使用を試みます。
filename
SQLite データベースへのパス。インメモリデータベースを使う場合は :memory: を指定します。
filename に空文字列を指定すると、
プライベート、かつ一時的なデータベースがディスク上に作成されます。
このプライベートなデータベースは、データベース接続が閉じられるとすぐに自動的に削除されます。
flags
SQLite データベースのオープン方法を指定するフラグ。
デフォルトでは SQLITE3_OPEN_READWRITE | SQLITE3_OPEN_CREATE
を使用してオープンします。
SQLITE3_OPEN_READONLY:
データベースを読み込み専用でオープンする
SQLITE3_OPEN_READWRITE:
データベースを読み書き共用でオープンする
SQLITE3_OPEN_CREATE:
データベースが存在しない場合は作成する
encryptionKeyオプションの暗号キー。SQLite データベースの暗号化と復号に使用します。 暗号化モジュールがインストールされていない場合は、このパラメータは何の影響も及ぼしません。
失敗した場合に Exception をスローします。
| バージョン | 説明 |
|---|---|
| 7.0.10 |
filename は、空文字列を指定できるようになりました。
この場合、プライベート、かつ一時的なデータベースがディスク上に作成されます。
|
例1 SQLite3::__construct() の例
<?php
$db = new SQLite3('mysqlitedb.db');
$db->exec('CREATE TABLE foo (bar TEXT)');
$db->exec("INSERT INTO foo (bar) VALUES ('This is a test')");
$result = $db->query('SELECT bar FROM foo');
var_dump($result->fetchArray());
?>Note that the SQLITE3_OPEN_READONLY flag cannot be combined with the SQLITE3_OPEN_CREATE flag. If you combine both of these flags, a rather unhelpful "Unable to open database: out of memory" exception will be thrown.$encryption_key and all encryption features will be enabled only if the SQLite encryption module is installed. It's a proprietary, costly module. So if it's not present, supplying an encryption key will have absolutely no effect.This constructor offers no built-in way to set the file's protection, for example, to allow group-wide read-write access to other users belonging to the www-data group. It is possible to create an empty file, then use chmod() to set the protection as needed. You can use code like this to create the file so it is group-writable (0664).
<?php
if ( !file_exists( $path ) ) {
touch( $path );
chmod( $path, 0664 );
}
$sqlite_handle = new SQLite3( $path, SQLITE3_OPEN_READWRITE | SQLITE3_OPEN_CREATE, '' );
?>
SQLite3 uses the same file protection on the .sqlite file you create for its other files, the .,sqlite-shm and .sqlite-wal files.