如何使SQLite在Laravel中工作 [英] How to make SQLite work in Laravel

查看:1303
本文介绍了如何使SQLite在Laravel中工作的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

每当我运行php artisan migrate时,控制台中都会显示以下错误:

Whenever I run php artisan migrate, the following error is shown in the console:

[PDOException]
SQLSTATE [HY000] [14]无法打开数据库文件

[PDOException]
SQLSTATE[HY000] [14] unable to open database file

database.sqlite 文件位于 database/中.我正在运行 Windows 10 Laravel 5.2 .这是 .env 文件配置:

The database.sqlite file is located at database/. I'm running Windows 10, Laravel 5.2. Here is .env file config:

DB_CONNECTION=sqlite
DB_HOST=127.0.0.1
DB_PORT=3306
DB_DATABASE=database
DB_USERNAME=homestead
DB_PASSWORD=secret

我到处都看过,但是找不到导致此错误的原因以及解决方法.

I have looked everywhere, but could not find what causes this error and how to resolve it.

我设法通过将 .env 文件中的DB_DATABASE=database替换为DB_DATABASE=database/database.sqlite来使迁移成功运行.但是,每当我尝试从数据库中检索项目时,都会发生新错误:

I managed to make migrations run successfully by replacing DB_DATABASE=database with DB_DATABASE=database/database.sqlite in .env file. However, new error occurs whenever I try to retrieve items from the database:

public function index()
{
    // cause of the error
    $cards = Card::all();

    return view('cards.index', compact('cards'));
}

上述操作引发以下错误:

The above action throws following error:

SQLiteConnector.php第34行中的InvalidArgumentException:
数据库(database/database.sqlite)不存在.

InvalidArgumentException in SQLiteConnector.php line 34:
Database (database/database.sqlite) does not exist.

奇怪的是,命令Card::all()php artisan tinker模式下可以完美地工作.那是什么魔术?

The strange thing is, that the command Card::all() works flawlessly in php artisan tinker mode. What kind of magic is that?

无论如何,我也发现了这一行:

Anyway, I've also found out, that the line:

'database' => env('DB_DATABASE', database_path('database.sqlite')),

需要将 database.php 文件中的

替换为database_path('database.sqlite'),并且一切开始正常工作.

in database.php file needs to be replaced with just database_path('database.sqlite') and everything starts to work normally.

看来,问题的根源是env('DB_DATABASE')调用.我转到 SQLiteConnector.php 文件,并转储了env('DB_DATABASE')database_path('database.sqlite')的输出.这分别是它们的输出:

It seems, that the root of the problem is env('DB_DATABASE') call. I went to SQLiteConnector.php file and dumped the output of both env('DB_DATABASE') and database_path('database.sqlite'). Here are their outputs respectively:

dd(env('DB_DATABASE'))               // => 'database/database.sqlite'
dd(database_path('database.sqlite')) // => 'D:\www\project\database\database.sqlite'

如您所见,它们的输出不同,第二个是预期的.这是Laravel的错误吗?还是我误会了什么?

As you see, their output differs, and the second one is what is expected. Is this a Laravel bug? Or did I misunderstood something?

推荐答案

简短解决方案

尽管没有回答问题,但解决找不到数据库" 问题的方法是替换 database.php 中的以下行:

Short Solution

Though not answering the question, the way to fix "Database not found" issue is to replace the following line in database.php:

'database' => env('DB_DATABASE', database_path('database.sqlite')),

'database' => database_path('database.sqlite'),

这篇关于如何使SQLite在Laravel中工作的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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