Page 1 of 1

Iterate on TUniTable with delete

Posted: Sun 06 Jul 2014 17:44
by testpresta
Hello

Look at this code:

Code: Select all

MyUniTable.first;
while not MyUniTable.eof do begin
    if (MyUniTable.FieldByName('some_field').asInteger>=300) then MyUniTable.delete;

     MyUniTable.Next;
end;
I want to delete some records of a table, that is accessing with a TUniTable component.

My question is: If i delete a record, does Next and Eof will work properly ?

Thanks

Re: Iterate on TUniTable with delete

Posted: Mon 07 Jul 2014 08:16
by AlexP
Hello,

Yes, on both deletion and insertion of records, the Next method and the Eof property work correctly..

Re: Iterate on TUniTable with delete

Posted: Mon 07 Jul 2014 09:06
by CristianP
If you delete a record the cursor will be positioned on the next record if there is one. If not (you deleted the last record) it will be positioned on the last record.
In this case I think you want something like this.

Code: Select all

MyUniTable.first;
while not MyUniTable.eof do begin
  if (MyUniTable.FieldByName('some_field').asInteger>=300) 
  then MyUniTable.Delete
  else MyUniTable.Next;
end;
Best regards,
Cristian Peta

Re: Iterate on TUniTable with delete

Posted: Mon 07 Jul 2014 14:43
by AlexP
Yes, the Delete method moves the pointer to the next record after the deleted one (if the deleted record is not the last one).