Reputation: 3701
[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 page or at StackOverflow.
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();
}
?>
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--
Upvotes: 13
Views: 13979
Reputation: 1
Check if your PHP installation has enabled extensions for sqlite and/or sqlite3.
I've been struggling with a similar problem only to find that it rejected my DSN because sqlite was disabled.
Upvotes: 0
Reputation: 332
Better than PDO('sqlite:...')
is to use PHP sqlite3
extension and its SQLite3
class. 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 SQLite3
class and... Hurray! It worked!
(Just for those who are wondering about PDO and a lot of its weird bugs.)
Upvotes: 0
Reputation: 98901
Just a small append to @justin answer:
You can also use the linux path style on windows:
$dsn = 'sqlite:c:/full/path/to/name.db';
Upvotes: 2
Reputation: 3701
Notice the simple slash in the DSN sqlite:/
? Just drop it !
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 !
Upvotes: 25