php 如何防止 SQLITE SQLSTATE[HY000] [14]?

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/25668156/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me): StackOverFlow

提示:将鼠标放在中文语句上可以显示对应的英文。显示中英文
时间:2020-08-25 17:56:36  来源:igfitidea点击:

How to prevent SQLITE SQLSTATE[HY000] [14]?

phppdosqlitepersistent

提问by Irgendw Pointer

I receive sometimes the following error:

我有时会收到以下错误:

SQLSTATE[HY000] [14] unable to open database file

SQLSTATE[HY000] [14] 无法打开数据库文件

I open the datebase by using

我使用打开数据库

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?

一段时间后(有时一个多小时,有时几分钟后,我在帖子开头收到错误消息。如何防止此类错误?

采纳答案by meda

This is an error from SQLlite :

这是来自 SQLlite 的错误:

#define SQLITE_CANTOPEN 14 /* Unable to open the database file */

#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.

好像你打开了很多连接,如果连接是打开的,我建议你重用连接。

Create a property:

创建属性:

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();
    }
}

回答by user2700551

If anyone is having the same message while reusing the PDO connection and still having problems, it might be because you're storing images you get from fopen() and forgot the fclose() statement. In this particular case, the error message is really misleading. Here's a question for the same error message that I managed to solve after a few days of troubleshooting. SQLSTATE[HY000] [14] : can't open database because too many connections are already opened

如果有人在重用 PDO 连接时遇到相同的消息但仍有问题,这可能是因为您正在存储从 fopen() 获得的图像而忘记了 fclose() 语句。在这种特殊情况下,错误消息确实具有误导性。这是我在几天的故障排除后设法解决的相同错误消息的问题。 SQLSTATE[HY000] [14] : 无法打开数据库,因为已经打开了太多连接

回答by Ayub

Very strange, but for me this was caused by not wrapping the new PDO statement in a try/catch block.

很奇怪,但对我来说这是因为没有将新的 PDO 语句包装在 try/catch 块中。