I’ve managed to create a SQLite DB in an iOS App by following some tutorials. I was initially expecting to rely on the primary key. At the time I didn’t know it was impossible to drop or make again the primary key in SQLite and now my App is huge and in order for it to work I need the primary key to stay “ordered”.
I’m developing a task manager as an exercise and I’m loading data directly from the DB to a TableView and when I delete a row I need the primary key index fixed.
Currently this is what happens when I delete a row:
ID TXT STAT
1 some_text 1
2 some_text 1
4 some_text 1
As you can see the primary key ID after I deleted the row is jumping a number, how can I make it to 1 2 3 etc… again?
I really don’t care if the solutions you present waste to much resources, this is only an exercise and I need it working 😀 (And I won’t make this mistake again!)
Thank you.
First, increase all IDs by an offset that is larger than any used ID:
Then set the ID to the number of records up to this one:
(The reason for the first
UPDATEis to avoid conflicts where the new ID of one record would be the same as the old ID of another.)