TQuery's efficiency

I use TQuery  as follows codes:

For i:=1 to 370 Do
        For j:=1 to 55 Do
    Begin

with Query_RGB do
     begin
     close;
     ParamByname('Colour_Name').asString:=Color_Name[1,i,j];
     prepare;
     open;
     while not Query_RGB.EOF do
         begin

             Color_RGB[1,i,j]:=Query_RGB.Fields[0].asInteger;
             Query_RGB.Next;

         end;
     end;


This Code cause a low efficiency,which cannot bear. How to improve it?

Thanks a lot
ws1999Asked:
Who is Participating?
 
rwilson032697Connect With a Mentor Commented:
<Lock>
0
 
rwilson032697Commented:
I'm not surprised! Thats a lot of TQuery opens!

It looks like you are going to visit every row in the table (or at least enough of them not to matter). So, use a plain TTable and iterate through it like this:

Table.First;
while not Table.eof do
  begin
    ConvertColourNameToIndex(Table.FieldByName['ColourName'].AsString, i, j);
    Color_RGB[1,i,j]:=Table.Fields[0].asInteger;
    Table.Next;
  end;

I hope this is clear. What is does is simply iterate through the records, and work out the i,j colour indexes from the colour name in each record, then doing the assignment to the color_rgb array.

Cheers,

Raymond.
0
 
geobulCommented:
Hi,
What does the Query_RGB.SQL contain?
And what tables do you read from?
Maybe you can decrease loops making SQL statement to give more records at once.

Regards, Geo
0
 
kotikCommented:
Hi!
Btw, you don't need to call Prepare every time you change params.
Prepare is used only if you change the SQL text. This will increase a little the efficiency:

Qry.Close;
Qry.SQL := YOUR_SQL_TEXT;
Qry.Prepare;
for i := 1 to 100 do
  for j := 1 to 100 do begin
    Qry.ParamByName(YourParam) := Value;
    Qry.Open;
    // Use Qry result
  end;
0
 
TAZICommented:
Hi . . .

What exactly are you trying to do here.  Please explain ? ?

Regards
Tazi
0
All Courses

From novice to tech pro — start learning today.