How to prevent SQLITE SQLSTATE[HY000] [14]?(如何防止 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/* 无法打开数据库文件 */
好像你打开了很多连接,建议你打开的连接重用.
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]?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持编程学习网!
本文标题为:如何防止 SQLITE SQLSTATE[HY000] [14]?


- 如何使用 Google API 在团队云端硬盘中创建文件夹? 2022-01-01
- Oracle 即时客户端 DYLD_LIBRARY_PATH 错误 2022-01-01
- openssl_digest vs hash vs hash_hmac?盐与盐的区别HMAC? 2022-01-01
- 如何在 Symfony2 中正确使用 webSockets 2021-01-01
- 使用 GD 和 libjpeg 支持编译 PHP 2022-01-01
- PHP - if 语句中的倒序 2021-01-01
- 覆盖 Magento 社区模块控制器的问题 2022-01-01
- PHP foreach() 与数组中的数组? 2022-01-01
- Laravel 5:Model.php 中的 MassAssignmentException 2021-01-01
- 如何从数据库中获取数据以在 laravel 中查看页面? 2022-01-01