PHP 8.4.24 Released!

SQLite3::__construct

(PHP 5 >= 5.3.0, PHP 7, PHP 8)

SQLite3::__construct 实例化 SQLite3 对象并打开 SQLite 3 数据库

说明

public function SQLite3::__construct(string $filename, int $flags = SQLITE3_OPEN_READWRITE | SQLITE3_OPEN_CREATE, string $encryptionKey = "")

实例化 SQLite3 对象并打开连接到 SQLite 3 数据库的连接。如果构建包括加密,那么将尝试使用密钥。

参数

filename

SQLite 数据库的路径,或 :memory: 使用内存数据库。如果 filename 是空字符串,那么将创建私有的临时磁盘数据库。一旦数据库连接关闭,这个私有数据库就会自动删除。

flags

可选的 flag,用于确定如何打开 SQLite 数据库。默认使用 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 ('This is a test')");

$result = $db->query('SELECT bar FROM foo');
var_dump($result->fetchArray());
?>

添加备注

用户贡献的备注 3 notes

up
12
jtreurniet at example dot com
9 years ago
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.
up
12
bohwaz
15 years ago
$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.
up
0
olliejones at gmail dot com
21 hours ago
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.
To Top