27

I created my database.sqlite file in the database folder. My .env file contents are :

DB_CONNECTION=sqlite
DB_HOST=127.0.0.1
DB_PORT=3306
DB_DATABASE=absolute\path\to\database\database.sqlite
DB_USERNAME=admin
DB_PASSWORD=

When I run php artisan tinker and DB::table('users')->get(); I get the info from the database.

My DatabaseController is:

class DatabaseController extends Controller
{
    public function main()
    {
        $users = DB::table('users')->get();

        return view('database',compact($users));
    }
}

Yet when I request /database path I get the error:

QueryException in Connection.php line 647: Database (database/database.sqlite) does not exist. (SQL: select * from "users")

UPDATE: A temporary fix is to change the database.php from the config folder:

  'connections' => [

    'sqlite' => [
        'driver' => 'sqlite',
        'database' => 'absolute\path\database\database.sqlite', 
        'prefix' => '',
    ],

Instead of using env('DB_DATABASE', database_path('database.sqlite')), which returns database/database.sqlite not my absolute path.

Alexandru Antochi
  • 1,295
  • 3
  • 18
  • 42
  • how do your run your server? `php artisan serve` or apache/nginx? – dparoli Mar 31 '17 at 12:43
  • 4
    Use the absolute path to the file in your `.env` – Joe Mar 31 '17 at 12:50
  • 3
    heh, solution for me was to remove the DB_DATABASE entry from my .env altogether. That way it defaulted to using the database_path() function that correctly allowed my app to interact with the database. – Chris Aug 04 '17 at 18:34

12 Answers12

51

You need to use full path, something like:

DB_DATABASE=/home/laravel-project/database/database.sqlite
Alexey Mezenin
  • 158,981
  • 26
  • 290
  • 279
  • 15
    simpler way is to use "../database/database.sqlite" – wafi Apr 12 '19 at 12:05
  • 1
    thats for mac and linux, but windows? – TuGordoBello May 02 '19 at 18:49
  • 1
    @wafi With Laravel 7.3, writing "../database/database.sqlite" works for the migration, but when trying to login/register, the database is not found. It's found if reverting to `database/database.sqlite`, but then `php artisan migrate` doesn't find it anymore, so the solution with full path the good one. – DevonDahon Mar 31 '20 at 12:44
  • 1
    @TuGordoBello for windows use full path replacing single backslash double backslash \\. Eg : DB_DATABASE=C:\\Project_Folder\\database\\database.sqlite – Sanuja Ariyapperuma May 08 '20 at 09:41
  • If you are using Ubuntu WSL in Windows the absolute path must include /mnt/c/absolutepath Stop php artisan serve, config:cache, config:clear, serve This is what worked for me. – El Sordo Mar 15 '23 at 04:05
  • Adding the absolute path was part of the fix, but also this answer was the other half https://stackoverflow.com/a/54975979/2105095 – Cole Geissinger Aug 24 '23 at 06:04
19

If you remove DB_DATABASE=... from your .env file and use the path in the config/database.php:

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

(if your database.sqlite file is in database/ folder), it will work, too.

gdfgdfg
  • 3,181
  • 7
  • 37
  • 83
11

I ran the following commands:

php artisan config:cache

php artisan config:clear

php artisan serve - restarted the server

Alexandru Antochi
  • 1,295
  • 3
  • 18
  • 42
7

For those who still face this issue: https://laracasts.com/discuss/channels/general-discussion/database-databasedatabasesqlite-does-not-exist-error-on-running?page=1

Since sqlite only require DB_CONNECTION=sqlite in .env file so you just remove the other:

DB_HOST
DB_PORT
DB_DATABASE
DB_USERNAME
DB_PASSWORD

then save and run migration again. This was how I solved the problem. Cheers!

davychhouk
  • 1,549
  • 2
  • 16
  • 20
6

In config/database.php file:

'sqlite' => [
              'driver' => 'sqlite',
              'database' => dirname(__DIR__).'/database/database.sqlite',
            ],

Then run following command:

php artisan config:cache
php artisan config:clear
Anastasios Selmani
  • 3,579
  • 3
  • 32
  • 48
Sarwar Sikder
  • 71
  • 1
  • 4
4

As Chris said in the comments, the main solution is to completely delete DB_DATABASE from the .env file (in fact, only the first line of the following code is needed).

DB_CONNECTION=sqlite
DB_HOST=127.0.0.1
DB_PORT=3306
DB_USERNAME=admin
DB_PASSWORD=
3

For Windows, you need to set up your path like this

DB_DATABASE="C:\\xampp\\htdocs\\project\\data\\database.db"
Gpak
  • 3,342
  • 2
  • 21
  • 19
2

✅ When using sqlite as your db, remove other DB env variables and it should work fine.

I faced the same problem and this solved it perfectly.

DB_DATABASE=sqlite

Remove the others.

Joseph Ajibodu
  • 1,093
  • 10
  • 14
1

I think, that the problem here was because of Homestead. Absolute path to the database.sqlite file on local machine is not the same as the virtual machine one has.

In my case a had to set:

DB_DATABASE=/home/vagrant/code/database/database.sqlite

Or, you can just comment out this line and you are ready to go.

#DB_DATABASE=
0

go to the PHP folder from your C: directory and open the file php.ini. From there find extension=pdo_sqlite and remove ;

0

Faced the same problem. Just had to create the database.sqlite file in the database directory and run my migrations

snaveware
  • 11
  • 2
0

Hello this has helped me. I hope it could help someone else too.

DB_DATABASE=storage/database.sqlite

this is in the .env file of course before that just create the file with

touch storage/database.sqlite
Denise Ignatova
  • 465
  • 4
  • 7