how do icheck if database exist with mydac ?

Discussion of open issues, suggestions and bugs regarding MyDAC (Data Access Components for MySQL) for Delphi, C++Builder, Lazarus (and FPC)
Post Reply
drama22
Posts: 6
Joined: Sun 24 Jul 2016 01:27

how do icheck if database exist with mydac ?

Post by drama22 » Tue 04 Oct 2016 13:31

how exactly i can return if database exits or not ? for example if its exist abort and don't do any thing .

ViktorV
Devart Team
Posts: 3168
Joined: Wed 30 Jul 2014 07:16

Re: how do icheck if database exist with mydac ?

Post by ViktorV » Tue 04 Oct 2016 14:23

If you want to define DB existence at the moment of connection, you can use the following code, having previously set the TMyConnection.Database property to the needed value:

Code: Select all

  MyConnection.Database := 'DBName';
  try
    MyConnection.Connect;
  except
    on E: Exception do
      if Pos('Unknown database', E.Message) <> 0 then
        ShowMessage('Database "DBName" does not exist')
      else
        raise;
  end;
If you want to define DB existence after connection to server is established, use the following code:

Code: Select all

  MyQuery.SQL.Text := 'SELECT IF(EXISTS (SELECT SCHEMA_NAME FROM INFORMATION_SCHEMA.SCHEMATA WHERE SCHEMA_NAME = ''DBName''), ''Yes'', ''No'') ';
  MyQuery.Open;
  if MyQuery.Fields[0].AsString = 'No' then
    ShowMessage('Database "DBName" does not exist');

drama22
Posts: 6
Joined: Sun 24 Jul 2016 01:27

Re: how do icheck if database exist with mydac ?

Post by drama22 » Tue 04 Oct 2016 14:45

the second statement raise error

Code: Select all

WHERE SCHEMA_NAME = '+variablename+'

ViktorV
Devart Team
Posts: 3168
Joined: Wed 30 Jul 2014 07:16

Re: how do icheck if database exist with mydac ?

Post by ViktorV » Tue 04 Oct 2016 15:02

To solve the problem, please use the following code:

Code: Select all

WHERE SCHEMA_NAME = ' + QuotedStr(variablename) +'

Post Reply