Laravel:连接到 sqlite 数据库抛出“PDOException:找不到驱动程序”

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

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

phpsqlitelaravel

提问by Terry

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

我正在将 Laravel 项目部署到共享主机并添加了一个 php.ini :

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:

在 phpinfo 中,我还可以看到加载了 sqlite 扩展。使用以下代码片段测试连接到数据库也有效:

<?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.

但是,当尝试使用 Laravel 连接到数据库时,应用程序向我抛出 PDOException 错误。

Appreciate any help on this. Thanks in advance!

感谢您对此的任何帮助。提前致谢!

回答by hakre

The exception you get:

你得到的例外:

PDOException: Could not find driver

PDOException: 找不到驱动程序

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:

每当找不到数据库的驱动程序时,就会由 PHP 的 PDO 提供。PDO 需要一个驱动程序来操作不同的数据库(例如 Sqlite、Mysql 等)。PDO 根据 DSN 查找驱动程序。在您的情况下,DSN 是:

sqlite:app/database/production.sqlite

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

对于驱动程序,只查看第一个冒号 (" :")之前的部分:

sqlite

The string sqliteis used to find the sqlite3 driver (see PDO_SQLITE DSNin the PHP manual).

该字符串sqlite用于查找 sqlite3 驱动程序(参见PDO_SQLITE DSNPHP 手册)。

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.

在所有 PDO 中只有一个地方抛出此异常(php 5.4 lxr),并且只有在 DSN 中的冒号之前的文本找不到驱动程序时才会抛出它。这也是您可以从错误消息中得到的结果。

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 extensionhas been loaded.

由于您已经完成了“如何确定是否在 PHP 中启用 PDO”的最佳答案中概述的检查,因此您已经确定 PDO sqlite扩展已加载。

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

只是为了说明这一点:包含 sqlite 的 PDO 驱动程序的 PHP 扩展被命名为pdo_sqlite您已检查加载。

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

我唯一的解释是您确实加载了扩展,但 PHP 无法加载驱动程序。根据 PHP 消息来源(php 5.4 lxr),这可能有两个原因:

  1. The PDO driver requires a different PDO API version
  2. PDO must be loaded before loading any PDO drivers
  1. PDO 驱动程序需要不同的 PDO API 版本
  2. 在加载任何 PDO 驱动程序之前必须加载 PDO

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

(从技术上讲还有第三个原因,但它应该被忽略:将驱动程序添加到 PDO 驱动程序的哈希表失败,这是非常内部的,与 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.

由于已加载扩展程序,但未找到驱动程序,我怀疑注册 PDO 驱动程序时出现问题。您应该检查 PHP 错误日志以了解启动错误。正如您所说的,您在共享主机上遇到了这个问题,您需要联系支持您找到 PHP 错误日志的支持。您正在寻找启动错误。有时这些错误也会显示在 Web 服务器的错误日志中,具体取决于使用的 PHP SAPI 接口。

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.

还请提供信息,phpinfo()以便提供更多指导。根据您提供的信息,您可以放心,尚未加载 PDO 的 sqlite 驱动程序。这是一个配置问题。