How in MyDac to test is a table exists?

Discussion of open issues, suggestions and bugs regarding MyDAC (Data Access Components for MySQL) for Delphi, C++Builder, Lazarus (and FPC)
Post Reply
piedsoftware
Posts: 7
Joined: Thu 14 Feb 2013 06:40
Location: Brisbane, Australia

How in MyDac to test is a table exists?

Post by piedsoftware » Thu 14 Feb 2013 06:53

Hi

I am new to MyDac, and I'm converting a project from Zeos. It is mostly straightforward so far, but one apparent lack in MyDac is an equivalent for the Zeos TZTable property Exists. This allows you to test if the TableName property corresponds with an existing table on the database without opening the component.

So, how can you simply test is a table exists with MyDac components?

TIA
Mark Patterson

skydvrz
Posts: 32
Joined: Tue 23 Feb 2010 23:49
Location: Kissimmee, Florida USA
Contact:

Re: How in MyDac to test is a table exists?

Post by skydvrz » Thu 14 Feb 2013 23:22

Just do a query (for the default schema):

show tables like 'agoodtablename'

and count the records. Zero records=invalid tablename.

You can enumerate all the tables in the default schema with:

show tables

Add the normal schema name syntax if you want a specific schema.

DemetrionQ
Devart Team
Posts: 271
Joined: Wed 23 Jan 2013 11:21

Re: How in MyDac to test is a table exists?

Post by DemetrionQ » Mon 18 Feb 2013 12:59

Hello.

You can use the following ways:

1) use the TMyConnection.GetTableNames method. For example:

Code: Select all

  var SL:TStringList;
...
  SL:=TStringList.Create;
  MyConnection1.Database:='Your_DataBase';
  MyConnection1.GetTableNames(SL);
  if SL.IndexOf('Your_Table')=-1 then
    ShowMessage('Your_Table not exists')
  else
    ShowMessage('Your_Table exists');
  SL.Free;
2) use the TMyMetaData component. For example:

Code: Select all

  //you already have a TMyMetaData component named MyMetaData1 on the form
  MyMetaData1.MetaDataKind:='Tables';
  MyMetaData1.Restrictions.Values['TABLE_SCHEMA']:='Your_DataBase';
  MyMetaData1.Restrictions.Values['TABLE_NAME']:='Your_Table';
  MyMetaData1.Open;
  if MyMetaData1.RecordCount=0 then
    showmessage('Your_Tabl Not Exists')
  else
    showmessage('Your_Tabl Exists');

piedsoftware
Posts: 7
Joined: Thu 14 Feb 2013 06:40
Location: Brisbane, Australia

Re: How in MyDac to test is a table exists?

Post by piedsoftware » Thu 12 Dec 2013 05:06

Thanks, guys. Sorry about the belated reply.

In the end I went with this:
qry := CreateQry('show tables');
result := Qry.Locate(Qry.Fields[0].FieldName, tblName, [loCaseInsensitive]);
Qry.Free;

It was closer to what we had before.

---
Mark

AndreyZ

Re: How in MyDac to test is a table exists?

Post by AndreyZ » Thu 12 Dec 2013 08:05

You can use such approach if it is more appropriate for you. Feel free to contact us if you have any further questions about MyDAC.

Post Reply