如何防止SQLITE SQLSTATE [HY000] [14]? [英] How to prevent SQLITE SQLSTATE[HY000] [14]?
问题描述
有时我会收到以下错误:
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 /* Unable to open the database file */
似乎您已经打开了许多连接,建议您重新使用已打开的连接.
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]?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!