Terry
Terry

Reputation: 41

Laravel: Connecting to sqlite database throws "PDOException: Could not find driver"

I am deploying a Laravel project to a shared hosting and added a php.ini with:

extension=pdo.so
extension=pdo_sqlite.so
extension=sqlite.so

In phpinfo, I could also see that the sqlite extensions are loaded. Testing to connect to the database with the following code snippets also worked:

<?php
try { 
    $dbh = new PDO("sqlite:app/database/production.sqlite");
    echo "Connected to database!";
}
catch(PDOException $e)
{
    echo $e->getMessage();
}

However, when trying to connect to the databse using Laravel, the application throws me the PDOException error.

Appreciate any help on this. Thanks in advance!

Upvotes: 2

Views: 7285

Answers (1)

hakre
hakre

Reputation: 197712

The exception you get:

PDOException: Could not find driver

is given by PHP's PDO whenever the driver to your database could not be found. PDO needs a driver to operate with different databases (e.g. Sqlite, Mysql, ...). And PDO looks for the driver based on the DSN. In your case the DSN is:

sqlite:app/database/production.sqlite

And for the driver is looked only in the part before the first colon (":"):

sqlite

The string sqlite is used to find the sqlite3 driver (see PDO_SQLITE DSN in the PHP manual).

There is only a single place in all PDO where this exception is thrown (php 5.4 lxr) and it only gets thrown in case for the text before the colon in the DSN no driver could be found. This is also what you can expect from the error message.

As you've already done the checks outlined in the best answer to "How to determine if PDO is enabled in PHP" you are already certain that the PDO sqlite extension has been loaded.

Just to make this clear: The PHP extension which contains the PDO driver for sqlite is named pdo_sqlite which you have checked to be loaded.

The only explanation I have is that you did load the extension but PHP was not able to load the driver. According to PHP sources (php 5.4 lxr) this can have exactly two reasons:

  1. The PDO driver requires a different PDO API version
  2. PDO must be loaded before loading any PDO drivers

(technically there is a third reason but it should be ignored: adding the driver to the hashtable of PDO drivers failed, this is pretty internal and has nothing to do specifically with PDO)

As the extension is loaded, but the driver is not found, I suspect there was a problem registering the PDO driver. You should checkout the PHP error log for startup errors. As you say you have this problem on a shared hoster you need to contact the support where you find the PHP error log. You're looking for startup errors. Sometimes these error are also shown in the error log of the webserver depending on which PHP SAPI interface is used.

Please also provide the information from phpinfo() so that additional guidance can be given. With the information you've provided you can rest assured that the sqlite driver for PDO has not been loaded. This is a configuration issue.

Upvotes: 3

Related Questions