How to restart auto increment id from 1 in Android SQLite database

I have an Android SQLite database and I inserted a few rows. After I deleted these rows, the identifier column will continue from the last identifier, and I would like to restart the count from 1.

+5
source share
3 answers

There is a sqlite_sequence table inside your .db file

Each row has two columns "name", which is the name of the table "seq" an integer indicating the current last value in this table

You can upgrade it to 0

, .

: SQLite Reset

+5

Try:

delete from sqlite_sequence where name='your_table';
+5

If you want to reset each RowId through a 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);

}

+1
source

All Articles