0

for read data from database server im using mysql query like this:

FDQuery1.SQL.Text := 'select * from `table` WHERE dcid=3;';
FDQuery1.Open;
memo1.Lines.Add( FDQuery1.FieldByName('value').AsString );

but when i have to use a loop for read data from mysql

i need a array to read and store data on it

i know in php its like this

$arr = array();
while($obj = $q->fetch_object() ){
    $arr[] = $obj;
}

but how can i do this syntax in Delphi ?

9
  • Look at e.g. First, Next, Eof. Commented Apr 17, 2018 at 6:20
  • i know seek on rows,my question is about the variable that store data on it. Commented Apr 17, 2018 at 6:30
  • Sorry, I missed that part. In PHP your array will hold the information of the whole record? If so, I do not know an exact equivalent in Delphi. Commented Apr 17, 2018 at 8:26
  • 1
    I dont think there is such thing in delphi. You could make a Record with the same fields as your row in the database, and make an array of that record type. But you need to fill it yourself using loops again. But why do you need an array ? Why not use FQuery1 in stead ? Commented Apr 17, 2018 at 8:48
  • 2
    FQuery1 is going to be busy anyway populating the array. It's got to be in use while the data is being read. This sounds very much like an XY question. Explain the X that's causing you to try to hack around it using Y. Commented Apr 17, 2018 at 20:41

3 Answers 3

2

You don't need to do FDQuery1.First as that is implicit in the FDQuery1.Open. However if you want to know how many records you have it's important to do a FDQuery1.Last; before using FDQuery1.RecordCount to get the true record count otherwise you might get strange results.

All you need to do get your data into a memo is this

FDQuery1.SQL.Text := 'select * from table WHERE dcid=3;';
memo1.Lines.clear;
FDQuery1.Open;
While not FDQuery1.Eof do
    begin
    memo1.Lines.Add( FDQuery1.FieldByName('value').AsString );
    FDQuery1.next;  
    end;
end;
FDQuery1.Close;

although a better solution with minimal exception handling is

FDQuery1.SQL.Text := 'select * from table WHERE dcid=3;';
memo1.Lines.clear;
try
try
   begin
     FDQuery1.Open;
    While not FDQuery1.Eof do
        begin
        memo1.Lines.Add( FDQuery1.FieldByName('value').AsString );
        FDQuery1.next;  
        end;
    end;
     end;
except
 on E : Exception do
     begin
       showmessage ('Exception class name = '+E.ClassName+ slinebreak
                 +  'Exception message = '+E.Message);
      end  //on E
end; //try-except

finally
 FDQuery1.Close;
end; //try-finally

You mention an array. This is not needed if you only want to put the data in a memo but if you did want to put the data in an array (a dynamic array of variants as you don't know at design time how many record elements you need, how many field elements you need or what type each field is) then you would use the following code.

(Note this is deliberately not optimised code as I was trying to make the process clear)

Const
  FirstRecordIndex = 0;
  FirstFieldIndex = 0;

Var 
  DataArray : Variant;
  TheRecordCount, TheFieldCount,
  RecordNumber, FieldNumber : integer;
  Data : variant;

begin
FDQuery1.SQL.Text := 'select * from table WHERE dcid=3;';
FDQuery1.Open;

FDQuery1.Last; //to get correct recordcount
TheRecordCount := FDQuery1.RecordCount;
TheFieldCount := FDQuery1.FieldCount;
FDQuery1.First; //go back to the beginning of the dataset

//set the dimensions of the 2D array of variants to hold data
DataArray := VarArrayCreate([FirstRecordIndex, TheRecordCount, FirstFieldIndex, TheFieldCount], varVariant ); //element can be of any type

//fill it
RecordNumber := -1;  //initialise record indexe to just before the start
While not FDQuery1.Eof do
        begin
        inc(RecordNumber); //point to next record element in the array
        for FieldNumber := FirstFieldIndex to TheFieldCount -1 do  //load all the fields of this record
           begin
           Data := FDQuery1.Fields[FieldNumber].asVariant; //get the data
           DataArray[RecordNumber, FieldNumber] := Data;  //put into array
           end;
        FDQuery1.next;  //get next record
        end; //while
    end;
    FDQuery1.Close;
end;

To get the data back again use

For RecordNumber := FirstRecordIndex  to  TheRecordCount -1 do
    For FieldNumber := FirstFieldIndex to TheFieldCount -1 do
            begin
            Data := DataArray[RecordNumber, FieldNumber] ;
           //do something with the data ie put into a memo
            end; 
Sign up to request clarification or add additional context in comments.

Comments

0

You can use this, i hope that help u:

FDQuery1.SQL.Text := 'select * from `table` WHERE dcid=3;';
FDQuery1.Open;
if FDQuery1.Active and (FDQuery1.RecordCount > 0) then
begin
    FDQuery1.First;
    While not FDQuery1.Eof do
    begin
        memo1.Lines.Add( FDQuery1.FieldByName('value').AsString );
        FDQuery1.next;  
    end;
end;

Comments

0

you can do this with array of array consept

for this first you must create your new type

type
  myArray = array [1..10] of integer;
  myData= array of myArray;

and then you must define your variable

var
  Records: myData;
  i:integer;

on,now we have aarray of array to save database records on it so

begin
FDQuery1.SQL.Text := 'select * from table WHERE type=3;';
FDQuery1.Open;
FDQuery1.First;

SetLength(Records ,FDQuery1.recordcount);//or maybe you need a do a select count(*) query first
i:=0;

While not FDQuery1.Eof do
  begin
        Records[i][1]:= FDQuery1.FieldByName('value1').AsString;
        Records[i][2]:= FDQuery1.FieldByName('value2').AsString;
        Records[i][3]:= FDQuery1.FieldByName('value3').AsString;
        Records[i][4]:= FDQuery1.FieldByName('value4').AsString;
        Records[i][5]:= FDQuery1.FieldByName('value5').AsString;
        Records[i][6]:= FDQuery1.FieldByName('value6').AsString;
    i:=i+1;
        FDQuery1.next;
  end;
    FDQuery1.Close;   
end;

now all of your selected database records are in your local variable you can go over the row and make new queries with FDQuery1...

Comments

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.