Change DB_CONNECTION to sqlite in your .env file.
Connecting Laravel to Sqlite in Laravel 5.2
Hi,
How can I connect to Sqlite db?
This is what I did that didn't work.
- Created project.
- Created
database.sqliteinside thedatabasefolder (database\database.sqlite). - Changed
'default' => env ('DB_CONNECTION', 'mysql'),to'default' => env ('DB_CONNECTION', 'sqlite'),in thedatabase.phpfile. - Ran
php artisan migrate. - Got an error.
What files do I need to modify to establish connection to an Sqlite DB?
Do I need to change something in the .env file?
Thanks
@spekkionu I changed DB_CONNECTION to sqlite but id didn't work, I got error
[InvalidArgumentException]
Database (homestead) does not exist.
.env File:
APP_ENV=local
APP_DEBUG=true
APP_KEY=base64:aHo4mgxZ3TscVv1w3O4SLu7KfvOHqyQhZCyEoIsh0Qs=
APP_URL=http://173.146.78.79
DB_CONNECTION=sqlite
DB_HOST=127.0.0.1
DB_PORT=3306
DB_DATABASE=homestead
DB_USERNAME=homestead
DB_PASSWORD=secret
CACHE_DRIVER=file
SESSION_DRIVER=file
QUEUE_DRIVER=sync
REDIS_HOST=127.0.0.1
REDIS_PASSWORD=null
REDIS_PORT=6379
MAIL_DRIVER=smtp
MAIL_HOST=mailtrap.io
MAIL_PORT=2525
MAIL_USERNAME=null
MAIL_PASSWORD=null
MAIL_ENCRYPTION=null
Then I tried changing DB_DATABASE=homestead to DB_DATABASE=sqlite but I got the same error.
[InvalidArgumentException]
Database (sqlite) does not exist.
Then I tried DB_DATABASE=database\database.sqlite as stated in the documentation but I got the following error.
[PDOException]
Could not find driver
Docmuentation: https://laravel.com/docs/5.2/database
Thanks
change the DB_DATABASE directive to DB_DATABASE=database, since that's what the database file you created is called.
@jaimelopez Changed DB_DATABASE to DB_DATABASE=databaseand got the same error
DB_CONNECTION=sqlite
DB_HOST=127.0.0.1
DB_PORT=3306
DB_DATABASE=database
DB_USERNAME=homestead
DB_PASSWORD=secret
ERROR:
[PDOException]
Could not find driver
Thanks
On what OS are you running this?
Also, try looking at the phpinfo() output and see what it says for pdo_sqlite.
I tried it in a different server (local Vagrant/homestead) and it worked but it looks like there are three things that would need to be changed in order to make Sqlite work in Laravel 5.2.31
1- In the database.php file, change the DB-CONNECTION as follow...
'default' => env('DB_CONNECTION', 'sqlite'),
2- In the .env file, change the DB_CONNECTION and the DB_DATABASE as follow...
DB_CONNECTION=sqlite
DB_HOST=127.0.0.1
DB_PORT=3306
DB_DATABASE=database/database.sqlite
DB_USERNAME=homestead
DB_PASSWORD=secret
@jaimelopez As I mentioned it above, it is now working but this is a different server. On the other serve (remote Ubuntu) I'm still getting the error [PDOException] could not find driver. When I run phpinfo() in the other server I don't see anything that says pdo_sqlite but when I run sqlite3 -version I get 3.8.2 2013-12-06 14:53:30 27392118af4c38c5203a04b8013e1ecfdbrwcend0d, so I'm assuming I have it installed.
I also ran sudo apt-get install sqlite3 libsqlite3-dev but nothing.
Any idea what could be wrong?
Thanks
Sqlite is available for the CLI, but not for PHP. You're going to need to install it. Run
$ sudo apt-get install php5-sqlite3
If you're running php7, you need to install php7.0-sqlite3.
After that restart apache or whatever web server you have.
@jaimelopez I ran sudo apt-get install -y php5-sqlite and it worked.
When I ran sudo apt-get install php5-sqlite3 I got the following message output...
Reading package lists... Done
Building dependency tree
Reading state information... Done
E: Unable to locate package php5-sqlite3
Do you have any idea why Taylor would change this type of stuff? Coming from him I'm pretty sure there is a reason but it makes our lives harder :).
Thanks a lot for all of your help.
That's right, it should be sqlite without the 3. Sorry for the confusion.
What do you mean that Taylor changed something? This was a server configuration problem.
No worries and happy coding!
@jaimelopez Well, in Laravel 5.1 you didn't have to modify your .env configuration at all, only the database.php file. Thanks a lot.
You can do either/or. Taylor defaulted to using the .env file, in that way it can be ignored using git, and your server credentials don't get published to the world if you have a github repository, for example.
You can still configure everything on the database.php folder if you do it like this 'default' => 'sqlite'. In that way, it won't grab anything from the .env file.
@jaimelopez First of all thank you for following alone. I tested it and I think the .env file is the only file that drives the data configuration. I tried changing the database.php file from 'default' => env('DB_CONNECTION', 'mysql'), to 'default' => 'sqlite', as you suggested but it didn't work. I guess the .env file is now the only place to configure it.
Doc: https://laravel.com/docs/5.2/database
DB_CONNECTION=sqlite
DB_DATABASE=database/database.sqlite
Thanks a lot Jaime.
I think an explanation of how the config and .env files work might clear things up a bit.
In the config files many of the values use the env function to look something like this env('DB_CONNECTION', 'mysql').
What that is doing is basically asking "Is there an environment variable called DB_CONNECTION? If there is give me that value. If there is not give me the value I passed in as a second parameter."
So if the environment variable is set it doesn't matter what is set as the second parameter as it will only be returned if the environment variable is not set.
These environment variable are actually just normal environment variables and putting them in the .env file is not the only way to set them. They could also be set at the OS level or in your server configuration. The .env is just a more convenient way to set these environment variables, especially at development time.
A simple example of what the env() function does would look like this.
function env($key, $default = null)
{
if (isset($_ENV[$key)) {
return $_ENV[$key];
}
return $default;
}
@spekkionu Thank you for the explanation, make sense. But I could have sworn that in previous versions (Laravel 5.1) Sqlite would work just by modifying the database.php config file to env('DB_CONNECTION', 'sqlite').
Thanks
That would have worked if there was no entry for DB_CONNECTION in the .env file.
I will post this link just for my own reference since it relates to the issue I was having.
I actually got this error as well when I tried fetching data in a view controller but was working fine in Tinker. Hard coding as described in this thread solved my issue.
Thank you all
I think most noobs would encounter the same problem i did (Over and over again). Yes it's fine to just change in the .env file: DB_CONNECTION=sqlite Because Laravel will check first the .env file and only if it can't find anything it will go check it the config\database.php for the default.
But !, When you make the change in the .env file, make sure to reopen the app (close the app (CTRL+C) and reopen it with "php artisan serve") because this is when the app will check the .env file again. This is the only thing that will make your changes to take effect.
@CodeJhonny or just type php artisan config:clear
@CodeJhonny Yes, later I learned that the key was to stop and re-serve every time a change is made to the .env file. Thank you for resurrecting this question since somebody else will benefit from the answer.
if you encounter:
[PDOException]
Could not find driver
Go to your PHP installation folder, open the php.ini file enable pdo_sqlite:
;extension=php_pdo_sqlite.dll
simply remove the semi-colon and save. Run your migration command and you are good to go!
Hello! Have you tried to change DB_CONNECTION in .env ?
like:
DB_CONNECTION="FULL PATH\database.sqlite"
DB_HOST=127.0.0.1
DB_PORT=3306
DB_DATABASE=
DB_USERNAME=
DB_PASSWORD=
I fixed mine like this, today.
thank you for help @fsdolphin
Hi There,
I had the same problem and I fixed editing the .env file. I had a new problem then, it said that database.sqlite does not exist. I made sure that I created it in the right place, and even deleted it and created it again, but still had the same problem. I found that following didn't work:
DB_CONNECTION=sqlite
DB_HOST=127.0.0.1
DB_PORT=3306
DB_DATABASE=database.sqlite
DB_USERNAME=
DB_PASSWORD=
So I found in another post that the right info for a sqlite configuration was:
DB_CONNECTION=sqlite
DB_FILE=database.sqlite
Restarted the server and it worked fine!!
I hope that helps anybody in the same situation I was.
The answer to this question is found here
In your config/database.php file.
'sqlite' => [ 'driver' => 'sqlite', 'database' => storage_path('database.sqlite'), 'prefix' => '', ], and in your .env file
DB_CONNECTION=sqlite
It is easier to watch in video: https://www.youtube.com/watch?v=FQAHKaZPcVc
DB_CONNECTION=sqlite DB_FILE=database/database.sqlite
try this it work with me
Please or to participate in this conversation.