java 如何在 Android SQLite 数据库中从 1 重新启动自动增量 ID

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/7339542/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me): StackOverFlow

提示:将鼠标放在中文语句上可以显示对应的英文。显示中英文
时间:2020-10-30 19:34:59  来源:igfitidea点击:

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

javaandroiddatabasesqlite

提问by Alex

I have an Android SQLite Database and I inserted some rows. After I deleted these rows the ID column continues from the last ID and I would like to restart the counting from 1.

我有一个 Android SQLite 数据库,我插入了一些行。删除这些行后,ID 列从最后一个 ID 开始,我想从 1 重新开始计数。

采纳答案by user634545

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

在您的 .db 文件中有一个名为 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

每行有两列 'name' 是表的名称 'seq' 一个整数,表示该表中当前的最后一个值

You can update it to 0

您可以将其更新为 0

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

但请注意,您的表是否使用此 ID 作为唯一标识符。

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

看看这个答案:SQLite Reset Primary Key Field

回答by JRL

Try:

尝试:

delete from sqlite_sequence where name='your_table';

回答by UmAnusorn

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

如果你想通过内容提供者重置每个 RowId 试试这个

    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);

}

}