Database creation command in Laravel 5.1 - php

In my project I want to create console commands for dropping and creating DB, just like in Symfony2 like database creation command in laravel console? The way described in the answer to that question works fine for dropping DB, but when I try to create it with this code:
$dbType = \Config::get('database.default');
$dbName = \Config::get('database.connections')[$dbType]['database'];
\DB::statement("CREATE DATABASE `$dbName`");
I get an exception: SQLSTATE[42000] [1049] Unknown database 'my_awesome_db_name'. Any ideas?

Laravel is trying to connect to MySQL and select the database my_awesome_db_name. This isn't possible, because that database name doesn't exist yet (you haven't created it), so MySQL fails on the internal USE my_awesome_db_name command.

Related

lightsail unable to find database

i am new at AWS lightsail, basically just following the instruction step by step create a instance, create a database named mypost, and upload all file to instance by filezilla, and then use potty to connect phpmyadmin, export local database into online database, but somehow when i test the link on browser it tell me Connection Error: SQLSTATE[HY000] [1049] Unknown database 'mypost' , i checked both phpmyadmin and database in the aws console all named mypost, i also switched to public mode as well, anyone know why is it?

How to avoid checking for MySQL connection in every request of Laravel 5.5?

I have developed a application which is using Elastic Search for frontend APIs and MySQL for admin backend.
Now the problem is if Laravel fails to connect with MySQL for any reason it starts giving 500 error in APIs as well. Please note that APIs routes are completely different (separate Middlewares and Namespace) from backend routes.
This is the error I am am getting when it fails to connect with MySQL:
Illuminate\Database\QueryException: SQLSTATE[HY000] [2002] Connection refused (SQL: select * from `setting__settings` where `name` = core::locales limit 1) in file /var/www/html/nykaatv_github/vendor/laravel/framework /src/Illuminate/Database/Connection.php on line 664
Here i want to mention that I am using asgardCMS.
Did any one faced a similar issue before. And if Laravel checks for MySQL connection of each request by default then what is the way to override it.
Thanks.

Cannot create postgres database with Doctrine

I have project in Symfony2 with doctrine. In one moment i loose ability to create/drop databases via console commands like
app/console doctrine:database:drop --force
app/console doctrine:database:create
app/console doctrine:schema:update --force
If i attemp to create database, doctrine give me next error:
[Doctrine\DBAL\Exception\ConnectionException]
An exception occured in driver: SQLSTATE[08006] [7] FATAL: database "motherhood" does not exist
So, i can create database in psql with user, written in symfony config. If i try to drop existing database via Doctrine, it gives me this:
SQLSTATE[55006]: Object in use: 7 ERROR: database "motherhood" is being accessed by other users
DETAIL: There are 1 other session using the database.
But there is no connection to this database
The quick fix for the problem is to create a database with the same name as user executing the command, because PostgreSQL must connect to a database, even when creating a new one.
The failure when dropping the database should be fixed in DoctrineBundle 1.4.
The failure to create the database will be fixed in doctrine/dbal in the very next release (2.5.2) as mentioned here: https://github.com/doctrine/DoctrineBundle/issues/402 . The problem will be fixed by always connection to the template database, which cannot be dropped.

SQLite3 / PDO - No such table though it does exist

I have a problem with an SQLite3 database where I can access it either with the sqlite3 command or with the PHPStorm built-in database manager but the application I am working on doesn't find the tables in it. It correctly connects to the database it seems.
This line of PHP causes the PDOException:
$query = "SELECT * FROM users";
$results = self::$app->db->query($query);
And the exception is simply SQLSTATE[HY000]: General error: 1 no such table: users. I am using the Slim framework, by the way.
I don't really know what to do as I am new to Slim as well as SQLite.
Thank you for your help :-)
The database that you have opened does not contain this table.
SQLite will happily open any file name; if it does not exist, it will create a new, empty database.
Check your database file name.
Thanks to the accepted answer that pointed me in the right direction.
I am using Symfony 4.1 and realized that the base directory for Symfony is the public directory (should be app in 2.8) so to open my database I had to do :
# file: PROJECT_ROOT/.env
DATABASE_URL="sqlite:///../my_super.db"
But then, every call to doctrine in a command (like doctrine:schema:update) must be called in a direct subfolder of the project, like so:
PROJECT_ROOT/bin$ ./console doctrine:schema:update --dump-sql

Laravel sqlite saying the database is "encrypted or is not a database"

I have a database file on my computer called testing.sqlite, which contains a copy of me database that I want to use for testing (rather than connecting to my ordinary MySQL production server).
I created an entry in the config/database file in laravel as instructed, setting it to access the sqlite file, and it all look right. But when I try and actually do anything, I get the error message:
[Illuminate\Database\QueryException]
SQLSTATE[HY000]: General error: 26 file is encrypted or is not a database (SQL: select * from sqlite_master where type = 'table' and name = vendor_alerts)
I can access the file using sqlite testing.sqlite from the command line, why can't laravel read it?
The problem is that the version of sqlite is different. Laravel uses sqlite3, wheareas the sqlite command on your console might be an earlier version. Run sqlite -version from the command line to check your version - is is probably 2 something. You need to install sqlite3, and you'll find that it is not compatible - i.e. sqlite3 testing.sqlite will produce the same error you are getting from laravel.
So, upgrade your command line sqlite version, and copy the data into a new sqlite3 database, and laravel will work without trouble.

Categories