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.
Якщо база даних має шифрування, то буде спроба використати
encryption_key (ключ шифрування).
filename
Шлях до бази даних SQLite, або рядок із записом
:memory:, щоб використовувати базу даних в пам'яті.
Якщо параметр filename є порожнім рядком, тоді
на диску буде створено приватну тимчасову база даних. Ця приватна база
даних буде автоматично видалена після закриття з'єднання з нею.
flags
Необов'язковий прапорець, що визначає, як саме відкривати базу даних
SQLite. Типово: SQLITE3_OPEN_READWRITE |
SQLITE3_OPEN_CREATE.
SQLITE3_OPEN_READONLY: Відкриває базу даних лише
для читання.
SQLITE3_OPEN_READWRITE: Відкриває базу даних для
читання та запису.
SQLITE3_OPEN_CREATE: Створює базу даних,
якщо вона не існує.
encryptionKeyНеобов'язковий ключ шифру, що використовується для шифрування та дешифрування бази даних SQLite. Якщо модуль шифрування 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 ('Це перевірка')");
$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.