当数据库位于不同的文件夹中时,PDOSQLite3连接失败


PDO SQLite3 connection failure when database is in different folder

我正在尝试从 php 连接到 sqlite3 数据库,但是当我想将我的数据库放置在方便且超出 Web 可访问空间的地方时,我遇到了一堵砖墙。当 db 文件与我的 php 脚本位于同一文件夹中时,它可以正常运行,但是当我将其放置在其他地方时 - 静默失败。

我写了一个简单的检查器,所以更容易理解我的意思

<?php
$files = array(
   'data.db',
    getcwd() . DIRECTORY_SEPARATOR . 'data.db',
   'some_inner_folder' . DIRECTORY_SEPARATOR . 'data.db',
    getcwd() . DIRECTORY_SEPARATOR . 'some_inner_folder' . DIRECTORY_SEPARATOR . 'data.db'
);
foreach($files as $file) {
    if(file_exists($file))
        echo $file, ' found<br/>';
    else
        echo $file, ' not found!<br/>';
    try {
        $db = new PDO('sqlite:host=' . $file);
        if($db)
            echo 'connection to ', $file, ' made succesfully<br/>';
    } catch (PDOException $error) {
        echo 'error connecting to ', $file, ' error message: ', $error->getMessage(), '<br/>';
    }
    $db = null;
}

输出结果是:

data.db found
connection to data.db made succesfully
C:'Web'data.db found
error connecting to C:'Web'data.db error message: SQLSTATE[HY000] [14] unable to open database file
some_inner_folder'data.db found
error connecting to some_inner_folder'data.db error message: SQLSTATE[HY000] [14] unable to open database file
C:'Web'some_inner_folder'data.db found
error connecting to C:'Web'some_inner_folder'data.db error message: SQLSTATE[HY000] [14] unable to open database file

C:''Web''data.db 和 C:''Web''some_inner_folder''data.db 具有相同的内容(文件复制和粘贴)

PHP 版本 5.3.6, 视窗 7 x64

PDO驱动程序mysql, sqlite, sqlite2

SQLite 库 3.7.4

我真的不明白为什么它不起作用。

问题已解决

$db = new PDO('sqlite:host=' . $file);

应该读成这样:

$db = new PDO('sqlite:' . $file);

事实证明pdo_sqlite不需要"host="

当我们为 Drupal 7 编写数据库层时,我们发现 PDO 有许多错误模式,只有例外才值得一试。 $driver_options[PDO::ATTR_ERRMODE] = PDO::ERRMODE_EXCEPTION;,这将使静默失败更加嘈杂。

您是否对

其他文件夹有权限问题。 它必须能够创建和写入文件(即您必须能够写入目录和文件)。