Miguel Borges
Miguel Borges

Reputation: 7659

SQLite unable to open database file: Laravel + Windows

I'm try use a sqlite database in my laravel project, in local environment for dev (Windows 8.1 with AMMPS), but when I try run a migrate:instal command, this error apeear:

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

My database config file (app/config/local/database.php):

<?php

return array(
    'default' => 'sqlite',

    'connections' => array(
        'sqlite' => array(
            'driver'   => 'sqlite',
            'database' => __DIR__ . '\..\..\database\production.sqlite',
            'prefix'   => '',
        ),
    ),
);

Upvotes: 5

Views: 16319

Answers (10)

mohamed.nabil
mohamed.nabil

Reputation: 1

At al most , You have to set the right path to your SQLite database file.

To mitigate the conflict between settings in .env file and database.php file in config folder, Just set database file path in database.php file and remove it from .env file. .env file just write your connection and remove other database attributes.

DB_CONNECTION=sqlite

second open config/database.php go to connections array and change database path to your database file path database_path('your database file path')

'connections' => [

    'sqlite' => [
        'driver' => 'sqlite',
        'url' => env('DATABASE_URL'),
        'database' => database_path('mydb.sqlite'),
        'prefix' => '',
        'foreign_key_constraints' => env('DB_FOREIGN_KEYS', false),
    ],

Upvotes: 0

R Sun
R Sun

Reputation: 1669

In my case, changing url: '%env(DATABASE_AUTH_URL)%' to url: '%env(resolve:AUTH_DATABASE_URL)%' fixed the issue

Upvotes: 0

Terry
Terry

Reputation: 317

You just need to make sqlite file available at the defined path. In larvel 5.7, I just used following command and run migration.

touch /absolute/path/of/db.sqlite

That will create an empty file at defined location and you are good to go.

Observation: In Larvel 5.7, .sqlite file should be already available at defined location.

For windows, right click and create new file with the defined name at defined location.

Upvotes: 2

Malek Ben el ouafi
Malek Ben el ouafi

Reputation: 1025

Please check the permissions of the directory that database file is stored in. chmod 777 YOU_APP/database

Upvotes: -2

Sazzadur Rahman
Sazzadur Rahman

Reputation: 2930

Well it may be late but I have also faced the problem in ubuntu environment, here exactly how I overcome this issue.

You have to pass your fully qualified sqlite file's path to your .env file, like following:

DB_CONNECTION=sqlite
DB_HOST=127.0.0.1
DB_PORT=3306
DB_DATABASE="/var/www/html/my-awesome-project/database/database.sqlite"
DB_USERNAME=homestead
DB_PASSWORD=secret

Here change the value of DB_DATABASE key to the exact location of your sqlite file in your computer's filesystem.

Tips: You can find your database's path for sqlite file by adding

dd(database_path('database.sqlite'));

this line of code in the top of config/database.php file and run php artisan migrate to terminal. You can find the exact location from there, copy it and put it to .envs DB_DATABASE key.

Don't forget to restart laravel server and remove dd(database_path('database.sqlite')); form database.php before testing.

Upvotes: 1

Nathan Macedo
Nathan Macedo

Reputation: 19

try change the file databaseName.sqlite permission database to writeable and readable. Maybe change permissions folder database also.

In the production server change this file database to private directory for security.

Upvotes: 1

user2130131
user2130131

Reputation: 21

On laravel 5.3.x

This was the only thing that worked for me:

Change DB_CONNECTION from "mysql" to "sqlite". Because this is an sqlite database, you can delete all the other DB_* items in that .env file.

APP_ENV=local APP_KEY=base64: APP_DEBUG=true APP_LOG_LEVEL=debug APP_URL=http://localhost

DB_CONNECTION=sqlite

Upvotes: 2

Said Bouigherdaine
Said Bouigherdaine

Reputation: 647

try to remove the DB_DATABASE="yourdatabase" variable in your .env file

Upvotes: 19

Dutchy
Dutchy

Reputation: 9

Change DB_CONNECTION from "mysql" to "sqlite". Because this is an sqlite database, you can delete all the other DB_* items in that .env file.

APP_ENV=local
APP_KEY=base64:
APP_DEBUG=true
APP_LOG_LEVEL=debug
APP_URL=http://localhost

DB_CONNECTION=sqlite

Upvotes: -3

french_dev
french_dev

Reputation: 2177

If the permissions allow to write on the folder just create the storage folders.

e-g, this how I fix the problem.

in config/database.php :

'connections' => [

  'sqlite' => [
      'driver'   => 'sqlite',
      'database' => storage_path('database/databaseName.sqlite'),
      'prefix'   => '',
  ],

Then if you run on your terminal php artisan migrate, it returns you the [PDOException] SQLSTATE[HY000] [14] unable to open database file

Create the path folder by your own, e-g in your terminal mkdir storage/database/databaseName.sqlite

Make sure the permissions allow you to write, then re-run the command php artisan migrate, it returns you the success message : Migration table created successfully.

Upvotes: 4

Related Questions