Clear/Set IndexFieldNames - protocol?

The stable tester's forum for ZeosLib 7.0.x series

Report problems concerning our Delphi 2009+ version and new Zeoslib 7.0 features here.
Post Reply
IbeDBob
Junior Boarder
Junior Boarder
Posts: 47
Joined: 27.05.2010, 21:04

Clear/Set IndexFieldNames - protocol?

Post by IbeDBob »

Hi All,

Is there a procedure for clearing and setting the IndexFieldNames?

I have the following code and when I want to Edit an account I have to clear that Index and after the Post I need to reset it. I just want to be sure I am not risking corruption to the Database. Is this OK?

If not, how should I be doing it?

[align=left]

Code: Select all

  tblAccounts.IndexFieldNames:='';
  tblAccounts.Edit;
  tblAccounts.FieldByName('CustName').AsString:='Tom';
  tblAccounts.Post;
  tblAccounts.IndexFieldNames:='Accounts Asc';
[/align]
Thanks
Thanks

Dyslexic Bob
marsupilami
Platinum Boarder
Platinum Boarder
Posts: 1939
Joined: 17.01.2011, 14:17

Post by marsupilami »

Hello IbeDBob,

why would you want to do that? Why not simply do

Code: Select all

  tblAccounts.Edit;
  tblAccounts.FieldByName('CustName').AsString:='Tom';
  tblAccounts.Post;
? Normally Zeos should update all internal structures on its own and SQLite will update its indices on its own too?
Best regards,

Jan
IbeDBob
Junior Boarder
Junior Boarder
Posts: 47
Joined: 27.05.2010, 21:04

Post by IbeDBob »

Thanks for the reply Jan, but if there is an active Index then a sequential edit will not work. The Index has to be cleared before the Edit. Let's say you need to change a bunch of Accounts by resetting a Status Boolean.

[align=left]

Code: Select all

tbl.IndexFieldNames:='Account Asc';
tbl.First;
while not (tbl.EOF) do
begin
  tbl.Edit;
  tblAccounts.FieldByName('BadCust').AsBoolean:=False; 
  tbl.Post;
  tbl.Next;
end;
[/align]

If the Index is active it will change ONLY that record and then place the file cursor at the end of the file so only one account gets changed. I have to disable the Index (and Filtered if active) in order to iterate through the Table.

In fact, if you change that loop to .Last, .Prior and .BOF it will loop there forever as it can never leave the EOF.

I am not sure if it is a Zeos thing or an SQLite thing, but I spent 3 hours yesterday trying to make it work. It was not until someone on another forum suggested clearing Indexes that it finally worked.

I would like to know if it is OK to just do as I wrote in code in the OP or whether there is some risk to the Database integrity doing that way.

Thanks
Thanks

Dyslexic Bob
marsupilami
Platinum Boarder
Platinum Boarder
Posts: 1939
Joined: 17.01.2011, 14:17

Post by marsupilami »

Hello IbeDBob,

I am not sure, wether it is a good Idea to directly manipulate the IndexFieldNames property. Maybe it is better to use the SortedFields property.

To make a long story short: AFAIK it should be possible to do as you describe because these indices are normally only used by Zeos to sort the rows.

but maybe it is better to use a simple SQL statement like
"update mytable set BadCust = False" and then to reopen the table instead of looping through all the records?

Best regards,

Jan
Post Reply