問題描述
我有時會收到以下錯誤:
I receive sometimes the following error:
SQLSTATE[HY000] [14] 無法打開數據庫文件
SQLSTATE[HY000] [14] unable to open database file
我使用
new PDO("sqlite:database/datbase.db","","",array(
PDO::ATTR_PERSISTENT => true
));
每次我想從數據庫讀取數據或向數據庫寫入數據時.打開過程是如下函數:
everytime I want read or write data from or to the database. The open process is the following function:
function opendatabase(){
try{
return new PDO("sqlite:database/database.db","","",array(
PDO::ATTR_PERSISTENT => true
));
}catch(PDOException $e){
logerror($e->getMessage(), "opendatabase");
print "Error in openhrsedb ".$e->getMessage();
}
}
一段時間后(有時一個多小時,有時幾分鐘后,我收到帖子開頭的錯誤消息.我該如何防止此類錯誤?
After some time (sometime more than an hour, some times after some minutes I get the error message at the beginning of the post. How can I prevent such error?
推薦答案
這是來自 SQLlite 的錯誤:
This is an error from SQLlite :
#define SQLITE_CANTOPEN 14/* 無法打開數據庫文件 */
好像你打開了很多連接,建議你打開的連接重用.
It seems like you have opened to many connections, I suggest you to reuse the connection if it is open.
創建屬性:
private $pdo;
并在創建新對象之前檢查它是否為空:
And check if it's null before creating a new object:
function opendatabase(){
try{
if($this->pdo==null){
$this->pdo =new PDO("sqlite:database/database.db","","",array(
PDO::ATTR_PERSISTENT => true
));
}
return $this->pdo;
}catch(PDOException $e){
logerror($e->getMessage(), "opendatabase");
print "Error in openhrsedb ".$e->getMessage();
}
}
這篇關于如何防止 SQLITE SQLSTATE[HY000] [14]?的文章就介紹到這了,希望我們推薦的答案對大家有所幫助,也希望大家多多支持html5模板網!