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
How in MyDac to test is a table exists?
-
piedsoftware
- Posts: 7
- Joined: Thu 14 Feb 2013 06:40
- Location: Brisbane, Australia
Re: How in MyDac to test is a table exists?
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.
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?
Hello.
You can use the following ways:
1) use the TMyConnection.GetTableNames method. For example:
2) use the TMyMetaData component. For example:
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;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?
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
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?
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.