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

发布于 2024-11-06 08:04:29 字数 1790 浏览 1 评论 0原文

我正在尝试从 php 连接到 sqlite3 数据库,但是当我想将数据库放置在方便且位于网络可访问空间之外的位置时,我遇到了困难。当 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,Windows 7 x64

PDO 驱动程序 mysql、sqlite、sqlite2

SQLite 库 3.7.4

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

解决的问题

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

应该是这样的:

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

结果 pdo_sqlite 不需要 'host='

I'm trying to connect to a sqlite3 database from php, but I'm hitting a brick wall when I want to place my db somewhere convenient and outside of web accesible space. When db file is in the same folder as my php script it runs ok, but when I place it somewhere else - silent fail.

I've written up a simple checker so it would be easier to understand what I mean

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

Output turned out to be:

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 and C:\Web\some_inner_folder\data.db has the same contents (file copy&paste)

PHP Version 5.3.6, Windows 7 x64

PDO drivers mysql, sqlite, sqlite2

SQLite Library 3.7.4

I'm not really seeing why it doesn't work.

Problem solved

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

should read like this:

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

Turns out pdo_sqlite doesn't need 'host='

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(2

请远离我 2024-11-13 08:04:29

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

When we were coding the database layer for Drupal 7 we found that PDO have many error modes and only exception is worth a damn. $driver_options[PDO::ATTR_ERRMODE] = PDO::ERRMODE_EXCEPTION; this will make the silent failure a lot more noisy.

嘿看小鸭子会跑 2024-11-13 08:04:29

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

Do you have a permissions problem on the other folder. It must be capable of creating and writing the file (ie you must be able to write to the directory and the file).

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文