Board index » delphi » Looping through the database

Looping through the database

Delphi experts,
   I am developing an application that will use a TDBgrid to access a
database table. Being new to Delphi and database programming, I am
having trouble putting together the code that would loop through all
records in the table, access a specific field of the current row, and
extract the field value as a string. I have investigated the help topics

and the Delphi manual regarding the TDBgrid object, but I have not been
able to assemble together the code logic to reach my goal. Although I
have used Paradox database tables for some time, I have not needed to
learn SQL or database programming techniques until this project. I'm
hoping that I can iterate through the table with purely traditional
Pascal statements. I envision this loop in "pseudo code" as:

Procedure ProcessColumnData;
var
   strvar: string;
   i: integer;
begin
   {DataGrid represents the TDBgrid object}
   for i := 0 to DataGrid.RowCount-1 do begin
      strvar := DataGrid.FieldName.Value;
     {do calculations with string here}
      .
      .
      .
   end;{for}
End;{procedure}

The crux of my problem is that I have not found a way to index the rows
of the DBgrid in the way, for example, that one can in a StringGrid
(i.e. the Cells[] array). Surely there is a similar way to do this with
DBgrid,

Thanks in advance,
   David

--
David T. Allison
Department of Geology and Geography
University of South Alabama
(334) 460-6381
http://www.usouthal.edu/usa/geography/allison/index.htm

 

Re:Looping through the database


On Wed, 15 Sep 1999 13:57:06 -0700, "David T. Allison"

Quote
<dalli...@jaguar1.usouthal.edu> wrote:
>Delphi experts,
>   I am developing an application that will use a TDBgrid to access a
>database table. Being new to Delphi and database programming, I am
>having trouble putting together the code that would loop through all
>records in the table, access a specific field of the current row, and
>extract the field value as a string. I have investigated the help topics

>and the Delphi manual regarding the TDBgrid object, but I have not been
>able to assemble together the code logic to reach my goal. Although I
>have used Paradox database tables for some time, I have not needed to
>learn SQL or database programming techniques until this project. I'm
>hoping that I can iterate through the table with purely traditional
>Pascal statements. I envision this loop in "pseudo code" as:

>Procedure ProcessColumnData;
>var
>   strvar: string;
>   i: integer;
>begin
>   {DataGrid represents the TDBgrid object}
>   for i := 0 to DataGrid.RowCount-1 do begin
>      strvar := DataGrid.FieldName.Value;
>     {do calculations with string here}
>      .
>      .
>      .
>   end;{for}
>End;{procedure}

To do this, use the underlying dataset component (TTable, TQuery, etc.)
rather than the TDBGrid. Use the dataset component's Eof property as the
basis for the loop. The Next method will move the record pointer to the
next record. Called from within the loop, that would be repeated until the
end of the file is reached. Use the Fields property or FieldByName method
of the dataset to get a reference to the field. Use the TField.AsString
property to retrieve the referenced field's value as a String.

  with Table1 do begin
    First;
    while not Eof do begin
      StrVar := FieldByName('TheField').AsString;
      Next;
    end;
    First;
  end;

==========================================================================
Steve Koterski                  "Computers are useless. They can only give
Technical Publications          you answers."
Borland                                       -- Pablo Picasso (1881-1973)
http://www.borland.com/techpubs/delphi

Re:Looping through the database


May I add to Steve's answer the following :

Table1.DisableControls;
try
    Table1.First;
    while not Table1.Eof do begin
      StrVar := Table1.FieldByName('TheField').AsString;
      Table1.Next;
    end;
    Table1.First;
finally
    Table1.EnableControls;
end;

Disabling then re-enabling the visual controls attached to your dataset
(through TDataSource) will greatly speed up the process, especially if you
have a lot of records (over a thousand). You won't see the progress of the
iteration, so if you want to know :

Table1.DisableControls;
try
    Table1.First;
    while not Table1.Eof do begin
      StrVar := Table1.FieldByName('TheField').AsString;
      Table1.Next;
       Label1.Caption := IntToStr(Table1.RecNo);
       Application.ProcessMessages;
    end;
    Table1.First;
finally
    Table1.EnableControls;
end;

If you have several thousand records (or several hundred thousand), enclose
the two extra lines in a conditionnal statement like so:

if Table1.RecNo mod 1000 = 0 then // or 100 for a smaller table
 begin
    Label1.Caption := IntToStr(Table1.RecNo);
    Application.ProcessMessages;
 end;

Keep in mind that RecNo works well with unfiltered Paradox tables. In any
other situation, you might want to use a counter rather than a while loop
i.e. " for i := 1 to Table1.RecordCount " and use the variable i instead of
Table1.RecNo. Then again, RecordCount doesn't always work either...

--

Alain Quesnel

P.S.: remove the [brackets] from my address if you wish to reply by e-mail.

Quote
David T. Allison wrote in message

<37E00821.B8C04...@jaguar1.usouthal.edu>...
Quote
>Delphi experts,
>   I am developing an application that will use a TDBgrid to access a
>database table. Being new to Delphi and database programming, I am
>having trouble putting together the code that would loop through all
>records in the table, access a specific field of the current row, and
>extract the field value as a string. I have investigated the help topics

>and the Delphi manual regarding the TDBgrid object, but I have not been
>able to assemble together the code logic to reach my goal. Although I
>have used Paradox database tables for some time, I have not needed to
>learn SQL or database programming techniques until this project. I'm
>hoping that I can iterate through the table with purely traditional
>Pascal statements. I envision this loop in "pseudo code" as:

>Procedure ProcessColumnData;
>var
>   strvar: string;
>   i: integer;
>begin
>   {DataGrid represents the TDBgrid object}
>   for i := 0 to DataGrid.RowCount-1 do begin
>      strvar := DataGrid.FieldName.Value;
>     {do calculations with string here}
>      .
>      .
>      .
>   end;{for}
>End;{procedure}

>The crux of my problem is that I have not found a way to index the rows
>of the DBgrid in the way, for example, that one can in a StringGrid
>(i.e. the Cells[] array). Surely there is a similar way to do this with
>DBgrid,

>Thanks in advance,
>   David

>--
>David T. Allison
>Department of Geology and Geography
>University of South Alabama
>(334) 460-6381
>http://www.usouthal.edu/usa/geography/allison/index.htm

Other Threads