Iterate on TUniTable with delete

Discussion of open issues, suggestions and bugs regarding UniDAC (Universal Data Access Components) for Delphi, C++Builder, Lazarus (and FPC)
Post Reply
testpresta
Posts: 32
Joined: Sat 07 Jun 2014 19:41

Iterate on TUniTable with delete

Post by testpresta » Sun 06 Jul 2014 17:44

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

AlexP
Devart Team
Posts: 5530
Joined: Tue 10 Aug 2010 11:35

Re: Iterate on TUniTable with delete

Post by AlexP » Mon 07 Jul 2014 08:16

Hello,

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

CristianP
Posts: 79
Joined: Fri 07 Dec 2012 07:44
Location: Timișoara, Romania

Re: Iterate on TUniTable with delete

Post by CristianP » Mon 07 Jul 2014 09:06

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

AlexP
Devart Team
Posts: 5530
Joined: Tue 10 Aug 2010 11:35

Re: Iterate on TUniTable with delete

Post by AlexP » Mon 07 Jul 2014 14:43

Yes, the Delete method moves the pointer to the next record after the deleted one (if the deleted record is not the last one).

Post Reply