TDbf.RecNo := xxx do not working properly
Original Reporter info from Mantis: Handoko
-
Reporter name: Handoko
Original Reporter info from Mantis: Handoko
- Reporter name: Handoko
Description:
I'm using:
Lazarus 0.9.26 Beta i386-win32 SVN 16885
FPC 2.2.2
DBFLaz 0.1.1
Hi, I found a bug in unit dbf (DBFLaz package).
If you insert some record, delete it, and insert some new record again. The data you read will not be correct. I have tried it several times using different data, I'm sure it always happen. Packing the table will solve the problem. And if you create a new table, insert some record and read it (without any deletion previously), you will get the correct data.
Strangely, TDBGrid will show the record correctly. I also try using CDBF Explorer (a freeware dbf utility), the record show correctly. So I think the problem only happend if I use coding to the manipulate the data. What I suspect is RecNo := xxx do not working properly.
Here, I explained it simply:
- A bug in TDbf (unit dbf, DBFLaz package)
- Record read incorrectly if there is any deleted record previously
- Packing the table can solve the problem
- Problem happen if you use coding to manipulate the record
- Problem will not happen if you use a TDBGrid to show the data
- I suspect TDbf.RecNo := xxx do not working properly
Of course, I can pack the table each time before I use coding to manipulate the data. But you know it is not a good solution.
Please a look at the additional information, don't forget to add unit dbf and db at the uses section. Or you can download the file I attach.
Additional information:
procedure TForm1.Button1Click(Sender: TObject);
var
DbfTest: TDbf;
Sum: Integer;
Z: Integer;
begin
DbfTest := TDbf.Create(nil);
with DbfTest do begin
FilePathFull := 'c:\';
TableName := 'Test.dbf';
FieldDefs.Add('QTY', ftInteger);
CreateTable;
Open;
// Insert data, then delete it
Insert; FieldByName('QTY').AsInteger := 20; Post;
Delete;
// Insert 20 and 15, so the summary should be 35
Insert; FieldByName('QTY').AsInteger := 20; Post;
Insert; FieldByName('QTY').AsInteger := 15; Post;
// Start calculating
Sum := 0;
for Z := 1 to ExactRecordCount do begin
RecNo := Z;
Sum := Sum + FieldByName('QTY').AsInteger;
end;
// But the result do not as expected
MessageDlg('Result', IntToStr(Sum), mtInformation, [mbOK], 0);
Close;
Free;
end;
end;
Mantis conversion info:
- Mantis ID: 13285
- Version: 0.9.26
- Target version: 2.7.1