Hi,
Can someone point out how I can find if a table exists in a database
using just myQuery? Or is there a method that checks if a table
exists within a database?
Thanks
check if table exists in a database
-
AndreyZ
Hello,
You can determine if a table exists in several ways:
1) by using the TMyQuery component:2) by using the GetTableNames method of the TMyConnection component:3) by using the TMyMetaData component:
You can determine if a table exists in several ways:
1) by using the TMyQuery component:
Code: Select all
procedure TMainForm.BitBtnClick(Sender: TObject);
begin
MyQuery.SQL.Text := 'SELECT * FROM information_schema.tables WHERE table_name = ''table_name''';
MyQuery.Open;
if not MyQuery.Eof then
ShowMessage('exists')
else
ShowMessage('not exists');
end;Code: Select all
procedure TMainForm.BitBtnClick(Sender: TObject);
var
str: TStringList;
begin
str := TStringList.Create;
MyConnection.GetTableNames(str);
if str.IndexOf('table_name') -1 then
ShowMessage('exists')
else
ShowMessage('not exists');
str.Free;
end;Code: Select all
procedure TMainForm.BitBtnClick(Sender: TObject);
begin
MyMetaData.MetaDataKind := 'Tables';
MyMetaData.Restrictions.Values['TABLE_NAME'] := 'table_name';
MyMetaData.Open;
if not MyMetaData.Eof then
ShowMessage('exists')
else
ShowMessage('not exists');
end;