Page 1 of 1

TMyQuery, read whole record at once?

Posted: Mon 13 Jan 2020 16:49
by docH
I am fetching data from a mySQL database using TMyQuery. Usually I process one field at a time inside a while not eof loop using fieldbyname().

As some processing is complex I'd like instead to fetch the whole record at once and pass that to a procedure to do the processing of each field away from the loop.

If this is possible to do, please can someone show me the syntax to fetch one whole record, pass it as a parameter and then extract each field?

The structure of the code I usually use is

Code: Select all

Procedure ProcessTable;
begin
    Query1.SQL.Clear;
    Query1.SQL.Add(SQL);
    Query1.Open;
    Query1.first ;
    while not Query1.Eof  do
       begin
           var1 :=  Query1.FieldByName('field1').AsString;
           var2 := Query1.FieldByName('field2').AsInteger;
           //etc.
           Query1.Next;
       end;
    Query1.close;  
end;
and the pseudo code of what I want to do is

Code: Select all

procedure ProcessRecord( TheRecord : <some type>);
begin
    ..
   //extract the fields from TheRecord and process them - how?
    var1 :=  TheRecord .FieldByName('field1').AsString;  - how?
    var2 := TheRecord .FieldByName('field2').AsInteger; - how?
    ..
end;

Procedure ProcessTable;
var
record : <some type>;
begin
    Query1.SQL.Clear;
    Query1.SQL.Add(SQL);
    Query1.Open;
    Query1.first ;
    while not Query1.Eof  do
       begin
       record:= Query1.<Get the whole record at once>; //how?
       ProcessRecord(record);
       Query1.Next;
       end;
    Query1.close
end;

Re: TMyQuery, read whole record at once?

Posted: Tue 14 Jan 2020 14:59
by davidmarcus
Why can't you do ProcessRecord( Query1 ) ?

Re: TMyQuery, read whole record at once?

Posted: Tue 14 Jan 2020 18:15
by docH
I didn't know I could.

But wouldn't that pass the whole query's data set to my procedure once for every record it looped through?
How would the procedure then extract the particular record it had just encountered - and how would I extract the fields from that record?

Is it possible you could post a bit of code showing how the called procedure would do it's processing to recover the field values from the record it had been passed?

Re: TMyQuery, read whole record at once?

Posted: Tue 14 Jan 2020 18:24
by davidmarcus

Code: Select all

procedure ProcessRecord( Query: TMyQuery );
begin
   ..
   var1 := Query.FieldByName('field1').AsString;
   var2 := Query.FieldByName('field2').AsInteger;
   ..
end;
The compiler will just pass the pointer to the TMyQuery object.

Re: TMyQuery, read whole record at once?

Posted: Tue 14 Jan 2020 19:49
by docH
Ah I see now. That's clever. I didn't appreciate the nature of the Query1 data type.
I presume when I pass the whole query it internally is passing a pointer to the current record which is why fieldbyname works.
That's going to make a lot of my code considerably easier to understand. Thank you

Re: TMyQuery, read whole record at once?

Posted: Wed 15 Jan 2020 07:02
by ViktorV
We are glad to see you have found a solution.
Feel free to contact us if you have any further questions about our products.