問題描述
我有時(shí)會收到以下錯誤:
I receive sometimes the following error:
SQLSTATE[HY000] [14] 無法打開數(shù)據(jù)庫文件
SQLSTATE[HY000] [14] unable to open database file
我使用
new PDO("sqlite:database/datbase.db","","",array(
PDO::ATTR_PERSISTENT => true
));
每次我想從數(shù)據(jù)庫讀取數(shù)據(jù)或向數(shù)據(jù)庫寫入數(shù)據(jù)時(shí).打開過程是如下函數(shù):
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();
}
}
一段時(shí)間后(有時(shí)一個多小時(shí),有時(shí)幾分鐘后,我收到帖子開頭的錯誤消息.我該如何防止此類錯誤?
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/* 無法打開數(shù)據(jù)庫文件 */
好像你打開了很多連接,建議你打開的連接重用.
It seems like you have opened to many connections, I suggest you to reuse the connection if it is open.
創(chuàng)建屬性:
private $pdo;
并在創(chuàng)建新對象之前檢查它是否為空:
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();
}
}
這篇關(guān)于如何防止 SQLITE SQLSTATE[HY000] [14]?的文章就介紹到這了,希望我們推薦的答案對大家有所幫助,也希望大家多多支持html5模板網(wǎng)!