Page 1 of 1
Clear/Set IndexFieldNames - protocol?
Posted: 25.03.2013, 14:24
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
Posted: 25.03.2013, 17:32
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
Posted: 25.03.2013, 18:47
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
Posted: 28.03.2013, 10:04
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