PHP PDO sqlite:无法打开数据库 [英] PHP PDO sqlite: Unable to open database

查看:85
本文介绍了PHP PDO sqlite:无法打开数据库的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我使用sqlite在php中开发一个webapp来将数据存储在数据库中。在互联网上看到我使用PDO而不是SQLITE3类。我做了互联网上显示的所有步骤,以避免出现此错误消息,但它仍然显示:

I develop a webapp in php using sqlite to store data in a database. As seen on the internet I use PDO instead of SQLITE3 class. I did all steps shown in the internet to avoid this error message, but it still apears:


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

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

定期任务每30秒运行一次,以获取表中的条目数。此任务由Javascript / Jquery触发。在普通的php文件上我使用PDO没有任何问题。但是将它与Javascript / Jquery结合使用我收到上面的消息。

A periodic task is running every 30 seconds to get the number of entries in a table. This task is triggered by Javascript/Jquery. On the normal php files I do not have any problem using PDO. But using it in combination with Javascript/Jquery I receive the message above.

我已经将整个路径和数据库设置为webserver的用户/组和chmod 0777 。

I set already the whole path and the database to user/group of the webserver and to chmod 0777.

创建自己编写的数据库类并将其存储在Session变量中,以避免在网页生命周期中创建和销毁。

My own written database class is created and stored in a Session variable, to avoid creation and destruction during the life of the webpage.

此函数连接到数据库(这个和下一个函数在同一个类中):

This function connects to the database (this and the next function are in the same class):

private $db = null;

private function connectdb(){
    try {
        $this->db = new PDO(database,"","",array(
                    PDO::ATTR_PERSISTENT => TRUE,
                    PDO::ERRMODE_EXCEPTION => TRUE));
    } catch (PDOException $e) {
        logerror($e->getMessage(), "connecthrsedb");
        return exception;
    }
}

此函数正在执行查询:

This function is doing the query:

function getNumberofEntriesinTable() {

    try {
        if (is_null($this->db)) {
            if ($this->connectdb() < 0) {
                return -1;
            }
        }

        $statement = "select count(*) from table;";

        $res = $this->db->query($statement);
        $res = $res->fetch();
        $res =  $res['count(*)'];
        $this->db = null;
        return $res;
    } catch (PDOException $e) {
        syslog(LOG_ERROR,$e->getMessage());
        return -1;
    }
 }

会话变量 $ _ SESSION ['database'] 在启动时生成。

The session variable $_SESSION['database'] is generated on startup.

这个javascript / jquery函数应该做的事情:

This javascript/jquery function should do the stuff:

function getData(){
    $.post("php/getdatafromdb.php",{action: 'getdata'},function(data){
        console.log(data);
    }
    setTimeout(function () {getData();},30000);
}

javascript / jquery函数运行良好并使用setTimeoutfunction执行。
但是我不知道如何预防或者删除这个问题。我该如何解决这个问题?

The javascript/jquery functions runs well and is executed with the setTimeoutfunction. However I don't know how to prevent or remove this issue. How can I solve this?

我的PHP ini文件中有关于sqlite和pdo的以下条目:

My PHP ini file is has the following entries concerning sqlite and pdo:

[sqlite]                                                                                       
; http://php.net/sqlite.assoc-case                                                             
;sqlite.assoc_case = 0                                                                         

[sqlite3]                                                                                      
;sqlite3.extension_dir =  
[Pdo]                                                                           
; Whether to pool ODBC connections. Can be one of "strict", "relaxed" or "off"  
; http://php.net/pdo-odbc.connection-pooling                                    
;pdo_odbc.connection_pooling=strict                                             

;pdo_odbc.db2_instance_name                                                     

[Pdo_mysql]                                                                     
; If mysqlnd is used: Number of cache slots for the internal result set cache   
; http://php.net/pdo_mysql.cache_size                                           
pdo_mysql.cache_size=2000                                                       

; Default socket name for local MySQL connects.  If empty, uses the built-in    
; MySQL defaults.                                                               
; http://php.net/pdo_mysql.default-socket                                       
pdo_mysql.default_socket=  

是否需要sqlite pdo设置?

Are there any sqlite pdo settings required?

推荐答案

我解决了这个问题。不幸的是,错误消息令人困惑,因为PHP没有找到该文件。
使用始终绝对路径,即使权限,用户和组设置正确,也要避免此错误。

I solved the problem. Unfortunatetly the error message is confusing, because PHP did not find the file. Use always absolute paths, to avoid this error even if permissions, user and group are set correct.

这篇关于PHP PDO sqlite:无法打开数据库的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆