aahoora's avatar

Database (database/database.sqlite) does not exist" error on running

Hi

I've created an sqlite database using command

php artisan make:migration create_cards_table --create=cards 

and

php artisan migrate 

All things was well and I inserted data in my database successfully.

but when I run my code to fetch data from database I faced with this error:

Database (database/database.sqlite) does not exist. (SQL: select * from "cards") 

Here is my .env file:

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

and here is my config/database.php file:

'default' => env('DB_CONNECTION', 'sqlite'),
'connections' => [

        'sqlite' => [
            'driver' => 'sqlite',
            'database' => env('DB_DATABASE', database_path('database.sqlite')),
            'prefix' => '',
        ],

and here is my code:

app/Http/Controllers/CardsController.php

class CardsController extends Controller
{
    public function index () {

        $cards = DB::table('cards')-> get();

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

    }
}

resources/views/cards/index.blade.php

@extends ('layout')

@section('content')
    @foreach($cards as $card)
        <div>{{ $card->title }}</div>

    @endforeach

@stop

How can I fix this?

0 likes
18 replies
bobbybouwmann's avatar

Did you create the database.sqlite file in the database directory?

After you did that you can migrate the database again

php artisan migrate
2 likes
vipin93's avatar

u can see your .sqlite file in your directory!

aahoora's avatar

@bobbybouwmann yes, the database.sqlite file is in database directory. I create that using command touch database/database.sqlite

The command

php artisan migrate

run correctly, and I inserted data to database using tinker but when I want to fetch data from database, it errors : Database (database/database.sqlite) does not exist. (SQL: select * from "cards")

aahoora's avatar

@bobbybouwmann Here it is :

class CreateCardsTable extends Migration
{
    /**
     * Run the migrations.
     *
     * @return void
     */
    public function up()
    {
        Schema::create('cards', function (Blueprint $table) {
            $table->increments('id');
            $table->string('title');
            $table->timestamps();
        });
    }

    /**
     * Reverse the migrations.
     *
     * @return void
     */
    public function down()
    {
        Schema::dropIfExists('cards');
    }
}
aahoora's avatar

the point is when I change .env file or config/database.php file and modify database path, the database path in error doen't change ! and still says:

Database (database/database.sqlite) does not exist. (SQL: select * from "cards") 

bobbybouwmann's avatar

Aah, do you use config caching?

You can clear your cache like so

php artisan cache:clear
aahoora's avatar

Here is the whole error :

InvalidArgumentException in SQLiteConnector.php line 34:
Database (database/database.sqlite) does not exist.
in SQLiteConnector.php line 34
at SQLiteConnector->connect(array('driver' => 'sqlite', 'database' => 'database/database.sqlite', 'prefix' => '', 'name' => 'sqlite')) in ConnectionFactory.php line 221
at ConnectionFactory->Illuminate\Database\Connectors\{closure}()
at call_user_func(object(Closure)) in Connection.php line 882
at Connection->getPdo() in Connection.php line 903
at Connection->getReadPdo() in Connection.php line 392
at Connection->getPdoForSelect(true) in Connection.php line 318
at Connection->Illuminate\Database\{closure}('select * from "cards"', array()) in Connection.php line 640
at Connection->runQueryCallback('select * from "cards"', array(), object(Closure)) in Connection.php line 607
at Connection->run('select * from "cards"', array(), object(Closure)) in Connection.php line 326
at Connection->select('select * from "cards"', array(), true) in Builder.php line 1718
at Builder->runSelect() in Builder.php line 1703
at Builder->get() in CardsController.php line 11
at CardsController->index()
at call_user_func_array(array(object(CardsController), 'index'), array()) in Controller.php line 55
at Controller->callAction('index', array()) in ControllerDispatcher.php line 44
at ControllerDispatcher->dispatch(object(Route), object(CardsController), 'index') in Route.php line 203
at Route->runController() in Route.php line 160
at Route->run() in Router.php line 559
at Router->Illuminate\Routing\{closure}(object(Request)) in Pipeline.php line 30
at Pipeline->Illuminate\Routing\{closure}(object(Request)) in SubstituteBindings.php line 41
at SubstituteBindings->handle(object(Request), object(Closure)) in Pipeline.php line 148
at Pipeline->Illuminate\Pipeline\{closure}(object(Request)) in Pipeline.php line 53
at Pipeline->Illuminate\Routing\{closure}(object(Request)) in VerifyCsrfToken.php line 65
at VerifyCsrfToken->handle(object(Request), object(Closure)) in Pipeline.php line 148
at Pipeline->Illuminate\Pipeline\{closure}(object(Request)) in Pipeline.php line 53
at Pipeline->Illuminate\Routing\{closure}(object(Request)) in ShareErrorsFromSession.php line 49
at ShareErrorsFromSession->handle(object(Request), object(Closure)) in Pipeline.php line 148
at Pipeline->Illuminate\Pipeline\{closure}(object(Request)) in Pipeline.php line 53
at Pipeline->Illuminate\Routing\{closure}(object(Request)) in StartSession.php line 64
at StartSession->handle(object(Request), object(Closure)) in Pipeline.php line 148
at Pipeline->Illuminate\Pipeline\{closure}(object(Request)) in Pipeline.php line 53
at Pipeline->Illuminate\Routing\{closure}(object(Request)) in AddQueuedCookiesToResponse.php line 37
at AddQueuedCookiesToResponse->handle(object(Request), object(Closure)) in Pipeline.php line 148
at Pipeline->Illuminate\Pipeline\{closure}(object(Request)) in Pipeline.php line 53
at Pipeline->Illuminate\Routing\{closure}(object(Request)) in EncryptCookies.php line 59
at EncryptCookies->handle(object(Request), object(Closure)) in Pipeline.php line 148
at Pipeline->Illuminate\Pipeline\{closure}(object(Request)) in Pipeline.php line 53
at Pipeline->Illuminate\Routing\{closure}(object(Request)) in Pipeline.php line 102
at Pipeline->then(object(Closure)) in Router.php line 561
at Router->runRouteWithinStack(object(Route), object(Request)) in Router.php line 520
at Router->dispatchToRoute(object(Request)) in Router.php line 498
at Router->dispatch(object(Request)) in Kernel.php line 174
at Kernel->Illuminate\Foundation\Http\{closure}(object(Request)) in Pipeline.php line 30
at Pipeline->Illuminate\Routing\{closure}(object(Request)) in TransformsRequest.php line 30
at TransformsRequest->handle(object(Request), object(Closure)) in Pipeline.php line 148
at Pipeline->Illuminate\Pipeline\{closure}(object(Request)) in Pipeline.php line 53
at Pipeline->Illuminate\Routing\{closure}(object(Request)) in TransformsRequest.php line 30
at TransformsRequest->handle(object(Request), object(Closure)) in Pipeline.php line 148
at Pipeline->Illuminate\Pipeline\{closure}(object(Request)) in Pipeline.php line 53
at Pipeline->Illuminate\Routing\{closure}(object(Request)) in ValidatePostSize.php line 27
at ValidatePostSize->handle(object(Request), object(Closure)) in Pipeline.php line 148
at Pipeline->Illuminate\Pipeline\{closure}(object(Request)) in Pipeline.php line 53
at Pipeline->Illuminate\Routing\{closure}(object(Request)) in CheckForMaintenanceMode.php line 46
at CheckForMaintenanceMode->handle(object(Request), object(Closure)) in Pipeline.php line 148
at Pipeline->Illuminate\Pipeline\{closure}(object(Request)) in Pipeline.php line 53
at Pipeline->Illuminate\Routing\{closure}(object(Request)) in Pipeline.php line 102
at Pipeline->then(object(Closure)) in Kernel.php line 149
at Kernel->sendRequestThroughRouter(object(Request)) in Kernel.php line 116
at Kernel->handle(object(Request)) in index.php line 53
at require_once('/var/www/html/laravel_proj/public/index.php') in server.php line 21
bobbybouwmann's avatar
Level 88

What happens if you remove the DB_DATABASE from your .env file and use the default in config/database.php which is

'database' => env('DB_DATABASE', database_path('database.sqlite')),
12 likes
tykus's avatar

As @bobbybouwmann says above; because you don;t need anything in the .env file except the DB_CONNECTION key/value pair. When you are using SQLite, remove all of the following keys:

DB_HOST
DB_PORT
DB_DATABASE
DB_USERNAME
DB_PASSWORD
3 likes
cab8021's avatar

This worked for me. Basically you just need to clear your .env file of all declarations needed for mysql. and then goto config/databsae.php and change

'default' => env('DB_CONNECTION', 'mysql'),

to

'default' => env('DB_CONNECTION', 'sqlite'),

Great Thanx!

1 like
ricardovigatti's avatar

Even that the @bobbybouwmann and @tykus answers are working solutions, the real reason about these problem is that SQLite databases should be declared with full path.

I'm using Xampp on Windows, so i have:

DB_DATABASE=C:\xampp7\htdocs\FooProject\database\database.sqlite

I think it's best to solve this way because i preffer to touch less on laravel's files

5 likes
alexman's avatar

@ricardovigatti is right. Also: If you're using the local/embedded php server, don't forget to restart it.. :)

3 likes
nalakaviraj's avatar

opening the database.sqlite from a text editor like notepad++ and saving it again as database.sqlite fixed my issue.

dandelionmood's avatar

@ricardovigatti is absolutely right — the absolute path is required for the connection on the web, for instance, but not to play the migrations for instance … All of which can be misleading in the end.

vrkansagara's avatar

The very simple solution for this issue as bellow.

'database' => database_path(sprintf("database-%s.sqlite3",config('app.env'))),
1 like

Please or to participate in this conversation.