I'm using a SQLite database in android that auto increments the _ID field, but when I remove a row from it, the _ID has a missing number so instead of it going from 1-9 it would go 1,2,4,5,6,7,8,9, but what I want it to do is go from 1 - 8! Is there a way to do this? Sorry about the question being confusing, I couldn't figure out how to phrase it!
1 回答
I'm using a SQLite database in android that auto increments the _ID field, but when I remove a row from it, the _ID has a missing number so instead of it going from 1-9 it would go 1,2,4,5,6,7,8,9, but what I want it to do is go from 1 - 8! Is there a way to do this?
By one word no. This what you are describing is normal behaviour. Internal iterator of autoincrement hasn't memory, simply said when once is generated id 3, it won't generate id 3 again but next number 4. If you should have generated ids 1,2,3,4,5 and now remove row with id 5, new inserted row will have automatic id 6 not 5.
In the most of DMSs this is normal behaviour. For sure, you can achieve it when you won't use autoincrement but explicitly added ids and when it changes so you can provide update of rows programatically but this approach is not recommended and also very "sick" because in the case you would have milion rows in table, you would update all milion rows and this operation is too much time-consume and especially for devices this is not good because they have limited memory and battery with certain capacity.
And also as @Areks mentioned if you are using relational tables you must also update references and it's just not worth it in most cases.
Note: If you are populating Adapter with List of own defined Objects you can simply save reference for row_id from SQLite into each Object and then when you want to delete row from ListView just get id from row and pass it into parameter of delete method.