PHP Conference Japan 2024

SQLite3::open

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

SQLite3::open開啟 SQLite 資料庫

描述

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

開啟 SQLite 3 資料庫。如果建置包含加密,則會嘗試使用金鑰。

參數

filename

SQLite 資料庫的路徑,或使用 :memory: 來使用記憶體內資料庫。

flags

用於決定如何開啟 SQLite 資料庫的可選旗標。預設情況下,open 會使用 SQLITE3_OPEN_READWRITE | SQLITE3_OPEN_CREATE

encryptionKey

加密和解密 SQLite 資料庫時使用的可選加密金鑰。如果未安裝 SQLite 加密模組,此參數將不起作用。

回傳值

沒有值會被回傳。

範例

範例 #1 SQLite3::open() 範例

<?php
/**
* 擴充 SQLite3 類別並變更 __construct
* 參數的簡單範例,然後使用 open 方法來初始化資料庫。
*/
class MyDB extends SQLite3
{
function
__construct()
{
$this->open('mysqlitedb.db');
}
}

$db = new MyDB();

$db->exec('CREATE TABLE foo (bar STRING)');
$db->exec("INSERT INTO foo (bar) VALUES ('This is a test')");

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

新增註解

使用者貢獻的註解 4 個註解

5
susan at itsasmartsolve dot co dot za
8 年前
[編輯註記:請參閱 <https://www.sqlite.org/backup.html>]

關於無法同時 open() 兩個資料庫並複製確切資訊的快速註解,例如為了建立備份資料庫。我搜尋網路尋找答案,但都沒有用。附加的註解和解決方案對我來說似乎很複雜。最後我發現可以使用 copy() 函式更簡單地達成相同的結果。
<?php
copy
("old.db", "new.db");
?>
2
BenWa
6 年前
如果您計畫並行存取 SQLITE3 資料庫,建議變更預設的 SQLite3::busyTimeout 值(預設設定為零)。否則,在寫入資料庫時可能會收到「資料庫已鎖定」錯誤。

將逾時設定為非零值時,對鎖定資料庫的寫入嘗試會在傳送錯誤之前等待鎖定解除(在逾時範圍內)。

請注意,SQLITE2 上的預設值設定為 60 秒。
2
jtreurniet at example dot com
7 年前
請注意,SQLITE3_OPEN_READONLY 旗標無法與 SQLITE3_OPEN_CREATE 旗標組合使用。如果您將這兩個旗標組合使用,將會擲回一個相當沒用的「無法開啟資料庫:記憶體不足」例外。
2
kbrobst at surveyresearchpartners dot com
14 年前
如果您嘗試使用 open() 方法在同一個 SQLite3 物件內開啟多個資料庫檔案(我無法使其運作),這裡有一種替代方法,可以使用 SQL 語言的特殊 SQLite3 語法附加項目來執行此操作。我花了一些時間研究,所以希望我找到的解決方案也能幫助您。

這些是 SQLite3 中利用的優良功能
* 資料表建立陳述式查詢儲存在父資料庫檔案中名為 "sqlite_master" 的資料表中。
* SQLite3 支援「insert into...select * from」SQL 語法,用於將資料批量快速插入資料表中 - 但是如果來源資料表和目標資料表位於不同的資料庫檔案中呢?
* SQLite3 有一個「attach [filename] as [參考資料庫名稱]」語法,允許開啟多個資料庫檔案,並讓同一個 SQLite3 物件可以存取。

假設您在 SQLite3 資料庫檔案 "source.db" 中有一個名為 "my_template" 的資料表。您想將此資料表的副本複製到資料庫檔案 "target.db" 中,並將該資料表命名為 "working_table"。

<?php
//將來源資料庫檔案附加到 bulkload 連線物件;
$bulkload_connection = new SQLite3("c:/sqlite3_database_files/source.db");

//取得來源資料表的建立陳述式查詢;
$sourcetbl_create_statement = $bulkload_connection->querySingle("select sql from sqlite_master where type='table' and name='my_template'");
if (
$sourcetbl_create_statement===false) exit($bulkload_connection->lastErrorMsg());

//建立目標資料表的建立陳述式查詢;
$targettbl_create_statement = str_replace('CREATE TABLE my_template', 'CREATE TABLE bulkload.working_table', $sourcetbl_create_statement);

//將目標資料庫檔案附加到 bulkload 連線物件 - 並將其作為名為 [bulkload] 的資料庫參考;
$result=$bulkload_connection->exec("attach 'c:/sqlite3_database_files/target.db' as bulkload");
if (
$result===false) exit($bulkload_connection->lastErrorMsg());

//發出查詢以在目標資料庫檔案內建立目標資料表;
$result=$bulkload_connection->exec($targettbl_create_statement);
if (
$result===false) exit($bulkload_connection->lastErrorMsg());

//盡快將資料列從來源資料表複製到目標資料表;
$result=$bulkload_connection->exec("insert into bulkload.working_table select * from my_template");
if (
$result===false) exit($bulkload_connection->lastErrorMsg());

//釋放附加資料庫檔案上的作業系統檔案鎖定;
$bulkload_connection->close();
unset(
$bulkload_connection);
?>
To Top