Page 1 of 1
Get fieldinfo
Posted: Mon 08 Mar 2010 08:13
by oz8hp
Is there an 'easy' way to get 2 lists from a db
List 1 = list of all found tables
List 2 = list of all found fields (with type, default value...)
Posted: Tue 09 Mar 2010 09:26
by Challenger
Please take a look at the TUniMetaData component. You can try the "Tables" and "Columns" values for the MetaDataKind property. Please note that the TUniMetaData component does not support retrieving of default values.
Posted: Tue 09 Mar 2010 09:49
by oz8hp
OK - I will give it a try (do you know of any samplecode that demonstrates the functions)
Posted: Wed 24 Mar 2010 09:28
by oz8hp
I have tried
procedure TfrmMain.btnMetadataClick(Sender: TObject);
var
Field: TStringField;
UniMetaData: TUniMetaData;
begin
UniMetaData := TUniMetaData.Create(nil);
try
UniMetaData.Connection := frmDB_Conn.conDBserver;
UniMetaData.Restrictions.Add('TABLE_NAME = tblsetup');
UniMetaData.MetaDataKind := 'Columns';
UniMetaData.Open;
while not UniMetaData.Eof do
begin
Field := UniMetaData.FieldByName('COLUMN_NAME') as TStringField;
lstFields.Items.Add(Field.Value);
UniMetaData.Next;
end;
finally
UniMetaData.Free;
end;
end;
But this gives me a list of all fieldnames in every db on the server.
How do I restrict it to one db and one table
Posted: Wed 24 Mar 2010 14:34
by Dimon
To solve the problem delete spaces in the restriction string, like this:
Code: Select all
UniMetaData.Restrictions.Add('TABLE_NAME=tblsetup');
Posted: Wed 24 Mar 2010 14:44
by oz8hp
OK - that was a small error
Now I will try and se if I can get it to show a list of tables in a db also.
(have been playing a bit with tha demo application supplied with uniDac - there are some errors when using MySQL on a Linux device. The tablenames are casesensitive and the name that the demo creates on the server and those used in code doesn't match. Just a comment)
Posted: Mon 29 Mar 2010 08:12
by oz8hp
I use the following code to show a list of tables in an MySQL db. But it doesn't work on MS SQL. What am I missing?
frmDB_Conn.conDBserver is my connection from my datamodule and Settings.DB_Database is a string containing the database name read from inifile.
procedure TfrmMain.FillTables;
var
Field: TStringField;
UniMetaData: TUniMetaData;
InfoList: TStringList;
begin
InfoList := TStringList.Create;
UniMetaData := TUniMetaData.Create(nil);
try
UniMetaData.Connection := frmDB_Conn.conDBserver;
UniMetaData.Restrictions.Add('TABLE_SCHEMA=' + Settings.DB_Database);
UniMetaData.MetaDataKind := 'Tables';
UniMetaData.Open;
while not UniMetaData.Eof do
begin
Field := UniMetaData.FieldByName('TABLE_NAME') as TStringField;
InfoList.Add(Field.Value);
UniMetaData.Next;
end;
cboTableNames.Items.Assign(InfoList);
finally
UniMetaData.Free;
InfoList.Free;
end;
cboTableNames.ItemIndex := 0;
end;
Posted: Mon 29 Mar 2010 08:55
by oz8hp
Actually the serverlist isn't available either if the server doesn't use named pipes as far as I can see.
Posted: Mon 29 Mar 2010 08:55
by Dimon
For MS SQL Server you should use the TABLE_CATALOG restriction instead of TABLE_SCHEMA, like this:
Code: Select all
UniMetaData.Restrictions.Add('TABLE_CATALOG=' + Settings.DB_Database);
Posted: Mon 29 Mar 2010 09:01
by oz8hp
OK - is there anyway to use UniScript here?
Posted: Mon 29 Mar 2010 09:59
by Dimon
oz8hp wrote:OK - is there anyway to use UniScript here?
I did not fully understand what you mean?
Posted: Mon 29 Mar 2010 10:10
by oz8hp
I just thought if it was possible to do something like when using uniscript for SQL
SQL.Add(' {IF SQLServer} ');
SQL.Add(' ALTER TABLE ' + aTable + ' ADD ' + aField );
SQL.Add(' {ELSE} ');
SQL.Add(' ALTER TABLE ' + aTable + ' ADD COLUMN ' + aField);
SQL.Add(' {endIF}');
but I use a Settings property wher I have the db type
Posted: Mon 29 Mar 2010 10:15
by oz8hp
But the named pipes is still an issue I think - I never turn that on unless I absolutly have to
Posted: Mon 29 Mar 2010 13:25
by Dimon
Yes, you can use TUniScript this way. You can find detailed information about macros usage in the "Unified SQL" topic of the UniDAC help.