TMyQuery, read whole record at once?

Discussion of open issues, suggestions and bugs regarding MyDAC (Data Access Components for MySQL) for Delphi, C++Builder, Lazarus (and FPC)
Post Reply
docH
Posts: 39
Joined: Sun 22 Dec 2013 15:18

TMyQuery, read whole record at once?

Post by docH » Mon 13 Jan 2020 16:49

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;

davidmarcus
Posts: 39
Joined: Tue 25 Jan 2005 11:22
Location: Somerville, MA
Contact:

Re: TMyQuery, read whole record at once?

Post by davidmarcus » Tue 14 Jan 2020 14:59

Why can't you do ProcessRecord( Query1 ) ?

docH
Posts: 39
Joined: Sun 22 Dec 2013 15:18

Re: TMyQuery, read whole record at once?

Post by docH » Tue 14 Jan 2020 18:15

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?

davidmarcus
Posts: 39
Joined: Tue 25 Jan 2005 11:22
Location: Somerville, MA
Contact:

Re: TMyQuery, read whole record at once?

Post by davidmarcus » Tue 14 Jan 2020 18:24

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.
Last edited by davidmarcus on Tue 14 Jan 2020 20:19, edited 1 time in total.

docH
Posts: 39
Joined: Sun 22 Dec 2013 15:18

Re: TMyQuery, read whole record at once?

Post by docH » Tue 14 Jan 2020 19:49

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

ViktorV
Devart Team
Posts: 2875
Joined: Wed 30 Jul 2014 07:16

Re: TMyQuery, read whole record at once?

Post by ViktorV » Wed 15 Jan 2020 07:02

We are glad to see you have found a solution.
Feel free to contact us if you have any further questions about our products.

Post Reply