0

In my app I have an option for the user to completely reset the app (remove all data/preferences they have set) so what I did when they select reset is to do a delete all in the ContentProvider

for example

getActivity().getContentResolver().delete(CalEvents.CONTENT_URI, null, null);

Doing this I thought the database now being cleared would start the id counter back at 1 but that is clearly not the case

The problem is that one of my tables used the database id 1-16 to look for data but when that table gets delete the next insert for that database would be 17-33 and that causes problems because I am looking for numbers 1-16 so it will never display data.

So part 1 of my question is, is it possible to get the table to re-index after I do a mass delete from it? I am guessing no but I do not know the answer.

If that is not possible how can I put out an update to the app where I can add a column to the table without deleting the data when it gets added? Currently I could increment the database version and the column would get added but it will delete all my data when the app updates and I do not want that

4

1 回答 1

1

根据这里的 SQLite 手册,您可以通过删除 sqlite_sequence 表中的条目来重新索引您的自动 id 字段,如下所示:

delete from sqlite_sequence where name='<Your Table Name Here>';

我自己没有这样做,但它应该可以工作。

编辑

SQLite 的手册中并不清楚每当发生新插入时都会重新创建表的条目,但它明确指出您可以更新 sqlite_sequence。因此,如果删除不能解决您的问题,您可以尝试像这样更新索引:

update sqlite_sequence set seq = 0 where name='<Your Table Name Here>';

希望这可以帮助...

于 2013-07-16T19:08:54.177 回答