Windows上php pdo的sqlite正确路径/URI [英] sqlite correct path/URI for php pdo on windows

查看:238
本文介绍了Windows上php pdo的sqlite正确路径/URI的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

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

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

我遇到了一个奇怪的配置问题,在特定的PHP.net页或在StackOverflow上.

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

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

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

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

?>

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

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

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

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

推荐答案

解决方案

注意DSN sqlite:/中的简单斜杠吗?放下它吧!

The solution

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

像这样编写您的DSN:

write your DSN like this :

sqlite:name.db.

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

This works with relative and absolute paths so :

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

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

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

希望这会为您节省一些时间!

Hope it will save you some time in the future !

这篇关于Windows上php pdo的sqlite正确路径/URI的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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