Database (database/database.sqlite) does not exist. Database works from artisan tinker
Asked Answered
M

15

35

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.

Michel answered 31/3, 2017 at 12:29 Comment(3)
how do your run your server? php artisan serve or apache/nginx?Marinamarinade
Use the absolute path to the file in your .envWhenas
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.Museum
C
61

You need to use full path, something like:

DB_DATABASE=/home/laravel-project/database/database.sqlite
Cynic answered 31/3, 2017 at 12:56 Comment(6)
simpler way is to use "../database/database.sqlite"Unpeopled
thats for mac and linux, but windows?Mcsweeney
@Unpeopled 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.Tain
@Mcsweeney for windows use full path replacing single backslash double backslash \\. Eg : DB_DATABASE=C:\\Project_Folder\\database\\database.sqliteScarcity
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.Takeoff
Adding the absolute path was part of the fix, but also this answer was the other half https://mcmap.net/q/422980/-database-database-database-sqlite-does-not-exist-database-works-from-artisan-tinkerFoist
P
22

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.

Pruter answered 29/5, 2017 at 15:15 Comment(0)
M
11

I ran the following commands:

php artisan config:cache

php artisan config:clear

php artisan serve - restarted the server

Michel answered 1/4, 2017 at 11:19 Comment(0)
B
8

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!

Bary answered 4/3, 2019 at 2:48 Comment(0)
G
7

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
Gamekeeper answered 13/12, 2018 at 11:22 Comment(0)
W
6

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=
Whomever answered 1/2, 2021 at 10:5 Comment(0)
O
4

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

DB_DATABASE="C:\\xampp\\htdocs\\project\\data\\database.db"
Odometer answered 16/3, 2021 at 8:11 Comment(0)
H
3

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

Holily answered 21/2, 2023 at 19:57 Comment(0)
T
2

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=
Triecious answered 7/1, 2019 at 0:29 Comment(0)
B
2

In your .env file replace DB_DATABASE = database.sqlite to DB_DATABASE = directory_path/project_name/database/database.sqlite

For example: directory_path: C:/xampp/htdocs/project_name/database/database.sqlite

It works fine for me.

Bur answered 15/2, 2024 at 4:57 Comment(0)
D
1

Firstly Run this commands:

  php artisan config:cache
  php artisan config:clear
  php artisan serve

Then, You need to use full path, something like:

DB_DATABASE=/home/laravel-project/database/database.sqlite

Configur In config/database.php file:

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

Make Sure

DB_DATABASE=sqlite
Discretional answered 5/1, 2024 at 16:22 Comment(0)
D
0

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

Disqualification answered 11/1, 2021 at 20:20 Comment(0)
P
0

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

Prestige answered 28/8, 2022 at 10:48 Comment(0)
T
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
Tetchy answered 28/6, 2023 at 12:22 Comment(0)
R
0

The solution is easier than it seems. Just install:

sudo apt install php-sqlite3

Okay, now you can run the command:

php artisan migrate

The only configuration required in .env is:

DB_CONNECTION=sqlite
Ryley answered 23/10, 2023 at 21:24 Comment(1)
Stack Overflow is an English-only site, but you can use the portuguese version of it: Stack Overflow em Português.Blackheart

© 2022 - 2025 — McMap. All rights reserved.