-2

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?

1 Answers1

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