Page 1 of 1

how do icheck if database exist with mydac ?

Posted: Tue 04 Oct 2016 13:31
by drama22
how exactly i can return if database exits or not ? for example if its exist abort and don't do any thing .

Re: how do icheck if database exist with mydac ?

Posted: Tue 04 Oct 2016 14:23
by ViktorV
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');

Re: how do icheck if database exist with mydac ?

Posted: Tue 04 Oct 2016 14:45
by drama22
the second statement raise error

Code: Select all

WHERE SCHEMA_NAME = '+variablename+'

Re: how do icheck if database exist with mydac ?

Posted: Tue 04 Oct 2016 15:02
by ViktorV
To solve the problem, please use the following code:

Code: Select all

WHERE SCHEMA_NAME = ' + QuotedStr(variablename) +'