如何将firedac tquery结果转换为tstringlist

时间:2017-09-04 04:54:55

标签: delphi firedac

有一个查询:

<?php
$isARequest = true;
if ($isARequest){include('request.html');}/*So because $isARequest is true then it will include request.html but if its not a request then it will insert isNotARequest;*/
else if (!$isARequest) {include('isNotARequest.html')}
?>

如何获得TQuery结果(多个id'字符串)并转换为TStringList?

2 个答案:

答案 0 :(得分:4)

当您在客户端上获取所有元组时,您可以通过这种方式访问​​内部存储(此时内部存储集合没有实现枚举器,因此这是您在使用它时需要编写的最小值):

var
  Row: Integer;
begin
  for Row := 0 to FDQuery.Table.Rows.Count - 1 do 
    StringList.Add(FDQuery.Table.Rows[Row].GetData(0)); { ← 0 here is the column index }
end;

如果您需要使用列名操作,可以使用另一个 GetData 方法重载:

var
  Row: Integer;
begin
  for Row := 0 to FDQuery.Table.Rows.Count - 1 do 
    StringList.Add(FDQuery.Table.Rows[Row].GetData('ID')); { ← ID here is the column name }
end;

或者只询问并记住列索引一次:

var
  Col, Row: Integer;
begin
  Col := FDQuery.Table.Columns.ColumnByName('ID').Index; { ← get column index by name }
  for Row := 0 to FDQuery.Table.Rows.Count - 1 do 
    StringList.Add(FDQuery.Table.Rows[Row].GetData(Col));
end;

GetData 存储访问具有不移动数据集光标的优势,最后,当使用 As&lt; T&gt;时,它与FireDAC用于获取数据的方式相同。 访问者(在这种情况下,隐式投放返回的变体)。

答案 1 :(得分:1)

yourStringList.Clear;
yourQuery.First;
while not yourQuery.Eof do
begin
 yourStringList.Add(yourQuery.FieldByName('id').AsString);
 yourQuery.Next;
end;