0

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"

2
  • I don't understand what you are asking about. You have declared the function to return TStringArray which again is declared as a dynamic 2D array. Use SetLength() to give the array length and width and fill in the values in your while loop. Look in help for dynamic arrays and SetLength. The result of a function has an implicit name of Result if that is the problem. Commented Dec 4, 2017 at 9:08
  • 1
    You'll maybe get an answer to your question, but the overall approach here is clumsy and most will probably feel that what you really need is to stop and think about your architecture a bit. If you just need to display the data then using a data-aware component is probably going to be much easier for you. Otherwise, if you're building a data model then you should probably work a bit more on the model part - a database row isn't an array of string, it's a record of typed data. Your data model should also probably consider it in that way. Commented Dec 4, 2017 at 12:41

2 Answers 2

3

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;
Sign up to request clarification or add additional context in comments.

Comments

0

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.

7 Comments

What? StringGrid? I don't see any mention of any grids in the question. This doesn't answer the question.
a, sorry. I Change this.
Better, but still wrong. At the very first iteration, after the SetLengths, how many lines and how many cols are there in the array? And at the second iteration, again after the SetLengths? Pay attention also to the indexes!
And being a guidedog is tiresome if the master constantly reacts only partially. I still can not remove my dv. How many array elements do you need to store data from e.g. 5 fields? Hau! Woff! Grrrrrrrr!
it could be easier if you correct the Errors you find. Sometimes the result is more important than the way ...
|

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.