C Matt
C Matt

Reputation: 35

How do I access the result of a Select count(*) using an SQLQuery component in Delphi

I am using Delphi RAD Studio 9 and Firebird 2.5

I want to use the count of the number of rows that fit a certain condition. When I put

Select count(*) from VRDB where Lname - 'SMITH'

into the the SQL property , upon opening SQLQuert1, I get the error message

SQLQuery1: Unable to determine field names for %s.

I assume this means Firebird or Delphi doesn't know what to do with the result.

How do I trap the result of the query? (My query statements work fine using isql.)

Upvotes: 0

Views: 7685

Answers (3)

ExperienceCounts
ExperienceCounts

Reputation: 11

No need to change your query string. Just reference FieldByName ('COUNT').AsInteger.

Upvotes: 0

MartynA
MartynA

Reputation: 30715

Using a Firebird database in Delphi 10 Seattle, the following works fine for me:

procedure TForm2.btnCountClick(Sender: TObject);
begin
  SqlQuery3.Sql.Text := 'select count(*) from maimages';
  SqlQuery3.Open;
  Caption := IntToStr(SqlQuery3.Fields[0].AsInteger);
end;

Btw, which Delphi version do you mean by "RAD Studio 9"? In case you mean Delphi 2009, the earliest Delphi version I have after D7 is XE4, and the above code also works fine with that.

Upvotes: 5

Maxim Votyakov
Maxim Votyakov

Reputation: 724

try to use

select count(*) CNT from VRDB where Lname = 'SMITH'

Upvotes: 1

Related Questions