Topic: MySQL Script

Hi to all,
I haven't found a script solution how to connect to MySQL without opening a database.
It would be cool if it could be possible to connect to MySQL, checking if a database is existing and if not to create it. Would make it easier for end-users.
Any ideas?

2 (edited by prahousefamily 2017-07-18 09:42:12)

Re: MySQL Script

Check Database have or haven't

SELECT SCHEMA_NAME FROM INFORMATION_SCHEMA.SCHEMATA WHERE SCHEMA_NAME = 'DBName'

Create Database If Haven't

CREATE DATABASE IF NOT EXISTS 'DBName';

Input  datebase name  if want check  in 'DBName'

My Visual Database : I Love You
Easy For Beginner Student For Me

Re: MySQL Script

prahousefamily wrote:

Check Database have or haven't

SELECT SCHEMA_NAME FROM INFORMATION_SCHEMA.SCHEMATA WHERE SCHEMA_NAME = 'DBName'

Create Database If Haven't

CREATE DATABASE IF NOT EXISTS 'DBName';

Input  datebase name  if want check  in 'DBName'

Works perfect with the internal SQLite.
But....
For connecting MySQL Server you need to have an exsting Database.
Only at design time you can create a new database.
I need this for connecting via script to MySQL.

Re: MySQL Script

Works perfect with the internal SQLite.
But....
For connecting MySQL Server you need to have an exsting Database.
Only at design time you can create a new database.
I need this for connecting via script to MySQL.



Example:

procedure Form1_OnShow (Sender: string; Action: string);
begin
     frmWait.Show;
     Application.ProcessMessages;

     {$MySQL disable_connectdialog}
     Form1.MySQLConnection.Server := '127.0.0.1';
     Form1.MySQLConnection.Port := 3306;
     Form1.MySQLConnection.Username := 'root';
     Form1.MySQLConnection.Password := '';


     try
         Form1.MySQLConnection.Connect;
     except
         frmWait.Close;
         ShowMessage('Can''t connect to database.');
         Form1.Close;
     end;

     if Form1.MySQLConnection.Connected then
     begin
         SQLExecute('CREATE DATABASE IF NOT EXISTS `DBName`;');
         SQLExecute('USE `DBName`;');
     end;
     frmWait.Close;
end;
Dmitry.