My Delphi application is connected to postgres database using firedac.I want to write a delphi function which can return result of select query.What should be the code and return type of the function?
Asked
Active
Viewed 1,340 times
-2
-
http://docwiki.embarcadero.com/RADStudio/Tokyo/en/First_Steps_to_Use_FireDAC – MBo Sep 21 '18 at 08:07
-
What will your function return ? A single value or a complete ResultSet ? You need to be more specific – GuidoG Sep 21 '18 at 08:41
-
function will return complete resultset. – Arati Padgaonkar Sep 21 '18 at 10:02
1 Answers
0
As described here you could do something like this.
function TMainForm.ReturnSomeData(AParamValue: string) : string;
begin
query.Close;
try
query.ParamByName('PARAMFIELD').AsString:= AParamValue;
query.Open;
if query.RecordCount > 0 then
Result query.FieldByName('FIELDNAME').AsString;
finally
query.Close;
end;
end;
above code is if you have placed TFDQuery in design time and place SQL in design time. If not you should create TFDQuery component on design time and fill query.SQL property.
if you have multiple values you should then use:
while not query.Eof do
begin
//save field value
query.Next;
end
UPDATE
function TMainForm.ReturnSomeData(AParamValue: string) : TStringList;
var
ExpoList: TStringList;
begin
query.Close;
try
query.ParamByName('PARAMFIELD').AsString:= AParamValue;
query.Open;
if query.RecordCount > 0 then
begin
ExpoList:= TStringList.Create;
while not query.Eof do
begin
ExpoList.Add(query.FieldByName('FIELDNAME').AsString);
query.Next;
end
Result:= ExpoList;
end;
finally
query.Close;
end;
end;

Mr. Nice
- 357
- 2
- 16
-
Thanks.Can you explain how to return complete ResultSet instead of single value? – Arati Padgaonkar Sep 21 '18 at 11:58
-