Gentlemen, enlighten, please, as in delphi programmatically get the number of fields of a particular database table? Interbase is used as a server. To connect to the database, the module is used with DQLConnection, SimpleDataSet, SQLQuery, DataSource placed on it.

datamodule3.SQLQuery1.SQL.Clear; datamodule3.SQLQuery1.Fields.Clear; datamodule3.SQLQuery1.SQL.Add(Format('select count(*) from NEW_TABLE',[])); datamodule3.SQLQuery1.Open; string1:=datamodule3.SQLQuery1.Fields[0].Value; 

This code returns 0.

  • So this means the number of records in the table = 0. What exactly do you need then? If the list of fields, then why 'count (*)'? - Chad
  • Sorry, the number of fields. - Vasily Koshelev

2 answers 2

If Interbase supports INFORMATION_SCHEMA, then:

 select COUNT(*) from INFORMATION_SCHEMA.COLUMNS where TABLE_NAME = 'NEW_TABLE' 

If not, you need to use system tables:

 SELECT count(*) FROM RDB$RELATION_FIELDS WHERE RDB$RELATION_NAME='NEW_TABLE'; 
  • Neither the first nor the second method does not work. In the first case - on ".Columns" In the second case, on "=" - Vasily Koshelev
  • Who is swearing? Delphi or Interbase? Is everything ok with quotes when you embed a request in the code? - msi
  • interbase. Yes, with quotes, everything should be fine. Checked. - Vasily Koshelev
  • The second method helped. The request should look like this: Format ('SELECT count (*) FROM RDB $ RELATION_FIELDS WHERE RDB $ RELATION_NAME =' + # 39 + 'NEW_TABLE' + # 39, []) - Vasily Koshelev

but perhaps the option:

 datamodule3.SQLQuery1.SQL.Clear; datamodule3.SQLQuery1.Fields.Clear; datamodule3.SQLQuery1.SQL.Add(Format('select * from NEW_TABLE limit 1',[])); datamodule3.SQLQuery1.Open; string1:=datamodule3.SQLQuery1.Fields.Length;//или Count не помню 

does not work?

But the truth is not sure that it will be faster than the @msi option. But on any database should work.

  • Sorry, does not work (At first swore on "1" in request. Removed it, after that returns empty value. - Vasily Koshelev
  • And what does "empty value" mean? limit 1 can, in principle, be removed; this is a limit on the number of records returned. - Chad