3

I need to iterate through a number of MySQL queries and save them in an array of TMemDataset's. This seems to do it:

MemDataset1.CopyFromDataset(ZQuery1,True); 

However each time the query changes, all the previous TMemDataset's are changed to contain the new values (I guess because they are "data-aware components"). If I get rid of ZQuery1 with ZQuery1.Free, then all of the data vanishes. How do I avoid this?

I am using FreePascal, but I bet the solution for Delphi would apply too.

da-soft
  • 7,670
  • 28
  • 36
Mike Furlender
  • 3,869
  • 5
  • 47
  • 75
  • 2
    'All previous TMem... contain the new vaues': Any chance that you are using 1 TMemDataset instance in your array? Can you show some code filling the array and creating TMemDatasets? – Arjen van der Spek Nov 20 '11 at 08:50
  • For me that looks like you have cloned TClientDataSet, not copied. Excluding this single line of your code, I have no idea, what you are doing there. Also you are using the technical terms incorrectly. Please make your question more constructive, by rephrasing your question and providing more code and details. – da-soft Nov 20 '11 at 19:45
  • First, mention FPC version. Did you test 2.6.0rc1 ? The second parameter is "copy", and if I look in the code, data is really copied. So the question is why it doesn't work for you (and why you use FPC SQLDB classes together with Zeos queries?) – Marco van de Voort Dec 21 '11 at 18:13

1 Answers1

1

The solution is to have an array of ZQuery as well as an array of MemDataSet

type
  TZQueries = array of TZQuery;
  TMemDataSets = array of TMemDataset;

procedure Test;
var
  ZQueries: TZQueries;
  MemDatasets: TMemDatasets;
  i: integer;
begin
  try
    SetLength(ZQueries,10);
    SetLength(MemDatasets,10);
    for i:= Low(ZQueries) to high(ZQueries) do begin
      ZQueries[i]:= TZQuery.Create;
      ZQueries[i].Connection:= ZConnection1;
      ZQueries[i].SQL.Text:= QueryTextFromSomewhere
    end; {for i} 
    for i:= Low(MemDatasets) to High(MemDatasets) do begin
      MemDatasets[i]:= TMemDataset.Create;
      ZQueries[i].Open;
      MemDatasets[i].CopyFromDataset(ZQueries[i],True);
    end; {for i}
    ....
      code to process the memdatasets
    ....
  finally
    for i = Low(ZQueries) to High(ZQueries) do begin
      ZQueries[i].Free;
    end; {for i}
    for i = Low(MemDatasets) to High(MemDatasets) do begin
      MemDatasets[i].Free;
    end; {for i}
  end; {tryf}
end;

Something like that should work, because now there's no more confusion between the Queries and the memdatasets.

Johan
  • 74,508
  • 24
  • 191
  • 319