Check if field exists

Discussion of open issues, suggestions and bugs regarding UniDAC (Universal Data Access Components) for Delphi, C++Builder, Lazarus (and FPC)
Post Reply
oz8hp
Posts: 151
Joined: Mon 18 Feb 2008 13:28
Location: Denmark
Contact:

Check if field exists

Post by oz8hp » Tue 27 Nov 2012 08:58

I have got the problem that I need to check if a field exists in a table but since the app runs on both MS Access and MySQL I need a way to do it in general.

(I use an old version 3 of UniDAC but company won't upgrade since this version works OK for our needs)

AndreyZ

Re: Check if field exists

Post by AndreyZ » Tue 27 Nov 2012 10:18

You can use the TUniMetaData component for this. Here is code example:

Code: Select all

procedure TForm1.Button1Click(Sender: TObject);
begin
  UniMetaData1.MetaDataKind := 'Columns';
  if AnsiSameText(UniMetaData1.Connection.ProviderName, 'MySQL') then // MS Access does not support schemas
    UniMetaData1.Restrictions.Values['TABLE_SCHEMA'] := 'databasename';
  UniMetaData1.Restrictions.Values['TABLE_NAME'] := 'tablename';
  UniMetaData1.Restrictions.Values['COLUMN_NAME'] := 'columnname';
  UniMetaData1.Open;
  if not UniMetaData1.Eof then
    ShowMessage('Field exists')
  else
    ShowMessage('Field does not exist');
end;

Post Reply