23

I have run my laravel app with phpunit. Everything is fine until at some point when I run my test again turns out with this error.

Illuminate\Database\QueryException: could not find driver (SQL: PRAGMA foreign_keys = ON;)

enter image description here

Caused by
PDOException: could not find driver

Here's my phpunit.xml file:

<phpunit xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
     xsi:noNamespaceSchemaLocation="./vendor/phpunit/phpunit/phpunit.xsd"
     bootstrap="vendor/autoload.php"
     colors="true">
<testsuites>
    <testsuite name="Unit">
        <directory suffix="Test.php">./tests/Unit</directory>
    </testsuite>
    <testsuite name="Feature">
        <directory suffix="Test.php">./tests/Feature</directory>
    </testsuite>
</testsuites>
<filter>
    <whitelist processUncoveredFilesFromWhitelist="true">
        <directory suffix=".php">./app</directory>
    </whitelist>
</filter>
<php>
    <server name="APP_ENV" value="testing"/>
    <server name="BCRYPT_ROUNDS" value="4"/>
    <server name="CACHE_DRIVER" value="array"/>
    <server name="DB_CONNECTION" value="sqlite"/>
    <server name="DB_DATABASE" value=":memory:"/>
    <server name="MAIL_MAILER" value="array"/>
    <server name="QUEUE_CONNECTION" value="sync"/>
    <server name="SESSION_DRIVER" value="array"/>
    <server name="TELESCOPE_ENABLED" value="false"/>
</php>
OS: Windows 10 | php version: PHP 7.4.11 | sqlite version: SQLite version 3.19.1

Does anyone encounter with this error in laravel 6 and phpunit?

Vic Andam
  • 823
  • 2
  • 7
  • 17

11 Answers11

60

If You face this problem

(could not find driver (SQL: PRAGMA foreign_keys = ON;))

You can simply run bellow command on your Ubuntu system

sudo apt-get install php-sqlite3

Also if you want to install specific version like php 8.1 simply run bellow command

sudo apt install php8.1-sqlite3

You might need to enable pdo_sqlite extension in your php.ini too, you can get the path of your loaded ini with the below command:

php --ini

simply add extension=pdo_sqlite to your php.ini and verify it is loaded by running

php -m | grep pdo_sqlite

I think it will help you.. Thanks

Eahiya
  • 931
  • 1
  • 6
  • 12
22

This is actually happening after I upgraded my php version in laragon. The solution is to enable pdo_sqlite on php extension in laragon. That's it. This answer found here https://laracasts.com/discuss/channels/general-discussion/phpunit-sqlite-error-after-updating-laragon-pdoexception-could-not-find-driver

Vic Andam
  • 823
  • 2
  • 7
  • 17
10

PHP 8.1

sudo apt install php8.1-sqlite3

Note that it needs the 8.1 part!

Sliq
  • 15,937
  • 27
  • 110
  • 143
5

This happens because PDO Sqlite is not enabled. to enable it in Laragon, just open Laragon click on menu > PHP > Extensions > pdo_sqlite That's it.

And if you don't use Laragon the solution might be different. Thanks

Hasibur Rahman
  • 641
  • 2
  • 7
  • 18
3

For those using windows;

Navigate to the php.ini file(-C:\php\php.ini)

Enable the following:

;extension=pdo_sqlite by removing the /;/ should look like this extension=pdo_sqlite

;extension=sqlite3 should be extension=sqlite3 without the ; symbol
  • Path to that ini file isn't necessarily always that `C:\php\php.ini` and can depend on your installation. You can run `php --ini` command in your terminal to get path to ini file for your current active PHP installation. – Marko Hologram Jun 24 '23 at 19:26
3

If you come across such an error,simply do the following;

1.run this command to install sqlite driver in your ubuntu or linux OS

sudo apt-get install php-sqlite3
  1. You also need to enable pdo_sqlite extension in your php.ini too, check the path of your loaded .ini with the below command

    php --ini

3.Then check and verify that the pdo_sqlite extension is loaded.

php -m | grep pdo_sqlite

Then the error will be solved.

2

just enabling these two extension from php.ini file worked for me.

extension=pdo_sqlite

extension=sqlite3
1

Just do the following changes on the php.ini file

extension_dir = "<php installation directory>/php-7.4.3/ext"
extension=php_pdo_sqlite.dll
extension=php_sqlite3.dll
sqlite3.extension_dir = "<php installation directory>/php-7.4.3/ext"
Saif
  • 9
  • 4
1

If you use Ubuntu try this:

php --version
sudo apt-get install PHP (your php version)-sqlite3

This works for me.

https://laracasts.com/discuss/channels/laravel/sqlite-database-throw-error-could-not-find-driver-sql-pragma-foreign-keys-on

ejuhjav
  • 2,660
  • 2
  • 21
  • 32
0

In case if you have multiple versions of PHP in Laragon you will still see the error. The solution is to enable pdo_sqlite extension on all versions. The reason is that Laragon has its own PHP but Laravel uses the system PHP and you might not know which one is system PHP version.

-1

To address the issue of encountering errors with multiple versions of PHP in Laragon, follow these steps:

  • Identify the presence of multiple PHP versions: Check if you have multiple PHP versions installed in your Laragon environment. Laragon has its own PHP, while Laravel relies on the system PHP.
  • Determine the system PHP version: It might be challenging to identify the specific version of the system PHP being used. To overcome this, proceed with the next steps.
  • Access the Laragon configuration: Open the Laragon configuration panel or settings. This can typically be found in the Laragon menu or system tray.
  • Enable the pdo_sqlite extension: Locate the option to enable or configure PHP extensions within the Laragon configuration panel. Look for the pdo_sqlite extension and ensure it is enabled for all PHP versions present in your Laragon setup.
  • Save and apply the changes: Once you have enabled the pdo_sqlite extension for all PHP versions, save the configuration changes.
  • Restart the Laragon services: To ensure the changes take effect, restart the Laragon services. This ensures that Laravel can properly utilize the updated PHP configuration.

Following these steps will ensure that the pdo_sqlite extension is enabled across all PHP versions in your Laragon environment. This resolves the issue of multiple PHP versions and allows for the seamless operation of the Laravel framework within Laragon.

Asif
  • 7
  • 5