Irgendw Pointer
Irgendw Pointer

Reputation: 1820

PHP PDO sqlite: Unable to open database

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] unable to open database file

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.

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

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

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

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

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?

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=  

Are there any sqlite pdo settings required?

Upvotes: 4

Views: 9884

Answers (1)

Irgendw Pointer
Irgendw Pointer

Reputation: 1820

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.

Upvotes: 6

Related Questions