开发者

How can I restart auto increment ID from 1 in an Android SQLite Database

开发者 https://www.devze.com 2023-04-03 02:54 出处:网络
I have an Android SQLite Database and I inserted some rows. After I deleted these rows the ID colu开发者_JAVA百科mn continues from the last ID and I would like to restart the counting from 1.Inside yo

I have an Android SQLite Database and I inserted some rows. After I deleted these rows the ID colu开发者_JAVA百科mn continues from the last ID and I would like to restart the counting from 1.


Inside your .db file there's an table called sqlite_sequence

Each row has two columns 'name' which is the name of the table 'seq' a integer indicating the current last value at this table

You can update it to 0

But beware if your table use this id as the unique identifier.

Take a look at this answer: SQLite Reset Primary Key Field


Try:

delete from sqlite_sequence where name='your_table';


If you want to reset every RowId via content provider try this

    rowCounter=1;do {           
        rowId = cursor.getInt(0);
        ContentValues values;
        values = new ContentValues();
        values.put(Table_Health.COLUMN_ID,
                   rowCounter);
        updateData2DB(context, values, rowId);
        rowCounter++;

    while (cursor.moveToNext());

public static void updateData2DB(Context context, ContentValues values, int rowId) {
Uri uri;
uri = Uri.parseContentProvider.CONTENT_URI_HEALTH + "/" + rowId);
context.getContentResolver().update(uri, values, null, null);

}

0

精彩评论

暂无评论...
验证码 换一张
取 消