Nevermore
Nevermore

Reputation: 1743

Delphi SQL return with multidimensional array

i created a type

type
  TStringArray = array of array of string;

function TDataModule4.GetList(TableName: String): TStringArray;
var
  i : Integer;
begin
  i:= 0;
  with TFDQuery.Create(Nil) do
  begin
    Connection := ADConnectionMySQL;
    SQL.Add(Format('Select * from %s', [TableName]));
    Open;
    First;
    while not Eof do
    begin

    end;
    Free;
  end;
end;

I know get data using FieldByName, but i want to return all data in multidimensional array. How can i do this?

Here is how i want to return :

array[0]["column1"] = "value1"

array[0]["column2"] = "value2"

array[1]["column1"] = "value3"

array[1]["column2"] = "value4"

array[2]["column1"] = "value5"

array[2]["column2"] = "value6"

Upvotes: 0

Views: 1354

Answers (2)

Victoria
Victoria

Reputation: 7912

Here is one way to do it (though I can't make sense for doing such a thing):

type
  TStringArray = array of array of string;
var
  Col: Integer;
  Row: Integer;
  Arr: TStringArray;
begin
  SetLength(Arr, FDQuery.Table.Rows.Count, FDQuery.Table.Columns.Count);

  for Row := 0 to FDQuery.Table.Rows.Count - 1 do
    for Col := 0 to FDQuery.Table.Columns.Count - 1 do
      Arr[Row][Col] := FDQuery.Table.Rows[Row].GetData(Col);
end;

Upvotes: 3

am2
am2

Reputation: 371

I did not use Delphi for a while and I never used TFD- Query, but as far as I remember this was no Problem. I wanted to write it to a comment, but sourcecode is better readable in an answer (untested). I introduced Q for the Name of your Query for better understanding and A for the array to be filled with data.

//var
A:TStringArray;
actLine, actCol:integer;

setlength(A,0);
actLine := 0;

while not Q.eof() do begin

  setlength(A,actLine + 1);
  setlength(A[actLine],FieldCount());

  for actCol := 0 to Q.FieldCount()-1 do
    A[actLine][actCol] := Q.Fields[actCol].AsString;

  inc(actLine);
  Q.Next;
end;

I hope, this helps you.

of course: array of array produces a 2 dimensional array with columns and lines numbered. Only the Content is string. A structure you describe you should implement yourself.

Upvotes: 0

Related Questions