windows windows上php pdo的sqlite正确路径/URI

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/8670687/
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-09-15 18:46:03  来源:igfitidea点击:

sqlite correct path/URI for php pdo on windows

phpwindowssqlitepathpdo

提问by Justin T.

[ante-scriptum : this is a self answered question, you don't need to bother answering]

[脚本前:这是一个自我回答的问题,你不需要费心回答]

I ran into a weird configuration problem, not documented anywhere on the specific PHP.net pageor at StackOverflow.

我遇到了一个奇怪的配置问题,没有在特定的 PHP.net 页面或 StackOverflow上的任何地方记录。

The problem

问题

When opening an existing sqlite database on Windows, the same error kept showing :

在 Windows 上打开现有的 sqlite 数据库时,一直显示相同的错误:

SQLSTATE[HY000] [14] Unable To Open Database File

SQLSTATE[HY000] [14] Unable To Open Database File

Although the code executed was copy/pasted from the manual :

虽然执行的代码是从手册中复制/粘贴的:

<?php
/* Connect to an ODBC database using driver invocation */
$dsn = 'sqlite:/full/path/to/db';
$user = 'dbuser';
$password = 'dbpass';

try {
    $dbh = new PDO($dsn, $user, $password);
} catch (PDOException $e) {
    echo 'Connection failed: ' . $e->getMessage();
}

?>

I could not open this database, as I had tried all kinds of various DSN while googling :

我无法打开这个数据库,因为我在谷歌搜索时尝试了各种不同的 DSN:

$dsn = 'sqlite:/c:\\full\\path\\to\\db'; // --FAILED--

$dsn = 'sqlite:/c:\\full\\path\\to\\db'; // --FAILED--

$dsn = 'sqlite://c:/full/path/to/db'; // --FAILED--

$dsn = 'sqlite://c:/full/path/to/db'; // --FAILED--

回答by Justin T.

The solution

解决方案

Notice the simple slash in the DSN sqlite:/? Just drop it !

注意到 DSN 中的简单斜线了sqlite:/吗?放下它!

write your DSN like this :

像这样写你的 DSN:

sqlite:name.db.

sqlite:name.db.

This works with relativeand absolutepaths so :

这适用于相对绝对路径,因此:

$dsn = 'sqlite:c:\full\path\to\name.db'; // --WORKS--

$dsn = 'sqlite:c:\full\path\to\name.db'; // --WORKS--

$dsn = 'sqlite:..\data\name.db'; // --WORKS--

$dsn = 'sqlite:..\data\name.db'; // --WORKS--

$dsn = 'sqlite:name.db'; // --WORKS--

$dsn = 'sqlite:name.db'; // --WORKS--

Hope it will save you some time in the future !

希望它会在未来为您节省一些时间!

回答by CONvid19

Just a small append to @justinanswer:

只是@justin答案的一个小附加:

You can also use the linux path style on windows:

您还可以在 Windows 上使用 linux 路径样式:

$dsn = 'sqlite:c:/full/path/to/name.db';

回答by Kubo2

Better than PDO('sqlite:...')is to use PHP sqlite3extension and its SQLite3class. I have tried your advices above with a database on an external (hard)drive and it didn't work (still giving the same error as mentioned in the first post by @justin-t). Then I tried with SQLite3class and... Hurray! It worked!

PDO('sqlite:...')使用 PHPsqlite3扩展及其SQLite3类更好。我已经在外部(硬盘)驱动器上的数据库上尝试了您的上述建议,但没有奏效(仍然出现与@justin-t 的第一篇文章中提到的错误相同的错误)。然后我尝试SQLite3上课,然后......万岁!有效!

(Just for those who are wondering about PDO and a lot of its weird bugs.)

(仅适用于那些想知道 PDO 及其许多奇怪错误的人。)

回答by Roberto Halys Ortu?o

Check if your PHP installation has enabled extensions for sqlite and/or sqlite3.

检查您的 PHP 安装是否启用了对 sqlite 和/或 sqlite3 的扩展。

I've been struggling with a similar problem only to find that it rejected my DSN because sqlite was disabled.

我一直在为一个类似的问题苦苦挣扎,却发现它拒绝了我的 DSN,因为 sqlite 被禁用了。