Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

How to make SQLite work in Laravel

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

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

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

.env:

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.

Update

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:

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

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')), 

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


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' 

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

like image 741
Alex Lomia Avatar asked Apr 02 '16 13:04

Alex Lomia


People also ask

Does laravel support SQLite?

Laravel supports many database connections such as MySQL, PostgreSQL, SQL Server and SQLite. Most of the time we use either MySQL or PostgreSQL. But if we need, we can easily change the default database connection of a Laravel application to SQLite. SQLite is a file based database and it is suitable for small projects.

How do I connect to a SQLite database?

To establish a database connection to an SQLite database, you need to create a new instance of the PDO class and pass a connection string to the constructor of the PDO object. Because you store the path to the sqlite database file in the Config class, you just simply use it to construct the connection string.


2 Answers

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')), 

with

'database' => database_path('database.sqlite'), 
like image 127
Alex Lomia Avatar answered Sep 29 '22 01:09

Alex Lomia


By using the relative path, migrations will fail because they use project directory as root directory...

To correct everything, I suggest setting:

   DB_DATABASE=database\database.sqlite 

and tweaking the sqlite connections in config/database.php as follows:

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

Original Answer

The .env file should contain this:

   DB_DATABASE=..\database\database.sqlite 

With some testing you can verify that the link included in DB_DATABASE is relative to the 'public' directory (at least on my Windows machine). That's why we should introduce ..\ before your link.

And of course, using an absolute link should do it too:

   DB_DATABASE=D:\www\project\database\database.sqlite  

as @Josh suggests

like image 44
motia Avatar answered Sep 28 '22 23:09

motia