how do i check if database exist or not 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
martinloanel
Posts: 2
Joined: Wed 24 Feb 2016 20:30

how do i check if database exist or not with mydac

Post by martinloanel » Wed 24 Feb 2016 20:38

in my application if there is database not exist application raise exception because data base not exist how do i check if data base exist ?

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

Re: how do i check if database exist or not with mydac

Post by ViktorV » Thu 25 Feb 2016 12:08

If you want to detect whether the database you are trying to connect to is created, you can use the following code:

Code: Select all

  MyConnection.Database := 'DBName';
  try
    MyConnection.Connect;
    ShowMessage('DBName exists.');
  except
    on E : Exception do
      if Pos('Unknown database', E.Message) <> 0 then
      begin
        ShowMessage('DBName not exists.');
      end;
  end;
You can also do this check by having connected to MySQL server (e.g., to an always existing system mysql database). Use the following code for this:

Code: Select all

procedure TForm.ButtonClick(Sender: TObject);
var
  Databases: TStringList;
begin
    MyConnection.Database := 'mysql';
    Databases := TstringList.Create;
    try
      MyConnection.GetDatabaseNames(Databases);
      if Databases.IndexOf('DBName') <> -1 then
        ShowMessage('DBName exists.')
      else
        ShowMessage('DBName not exists.');
    finally
      Databases.Free;
    end;
end;

martinloanel
Posts: 2
Joined: Wed 24 Feb 2016 20:30

Re: how do i check if database exist or not with mydac

Post by martinloanel » Thu 25 Feb 2016 14:56

thank you very much

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

Re: how do i check if database exist or not with mydac

Post by ViktorV » Fri 26 Feb 2016 10:43

If you have any questions during using our products, please don't hesitate to contact us - and we will try to help you solve them.

Post Reply