SQL SQLite 重置主键字段

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/1601697/
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-09-01 04:06:43  来源:igfitidea点击:

SQLite Reset Primary Key Field

sqlsqlite

提问by Nathan

I have a few tables in SQLite and I am trying to figure out how to reset the auto-incremented database field.

我在 SQLite 中有几个表,我想弄清楚如何重置自动递增的数据库字段。

I read that DELETE FROM tablenameshould delete everything and reset the auto-incremement field back to 0, but when I do this it just deletes the data. When a new record is inserted the autoincrement picks up where it left off before the delete.

我读到DELETE FROM tablename应该删除所有内容并将自动增量字段重置回0,但是当我这样做时它只会删除数据。当插入新记录时,自动增量会从删除前停止的位置开始。

My identfield properties are as follows:

我的ident字段属性如下:

  • Field Type: integer
  • Field Flags: PRIMARY KEY, AUTOINCREMENT, UNIQUE
  • 字段类型integer
  • 字段标志: PRIMARY KEY, AUTOINCREMENT,UNIQUE

Does it matter I built the table in SQLite Maestro and I am executing the DELETEstatement in SQLite Maestro as well?

我在 SQLite Maestro 中构建了表并且我也在 SQLite Maestro 中执行DELETE语句是否重要?

Any help would be great.

任何帮助都会很棒。

回答by Nick Dandoulakis

Try this:

尝试这个:

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

SQLite Autoincrement

SQLite 自动增量

SQLite keeps track of the largest ROWID that a table has ever held using the special SQLITE_SEQUENCEtable. The SQLITE_SEQUENCEtable is created and initialized automatically whenever a normal table that contains an AUTOINCREMENT column is created. The content of the SQLITE_SEQUENCE table can be modified using ordinary UPDATE, INSERT, and DELETE statements. But making modifications to this table will likely perturb the AUTOINCREMENT key generation algorithm. Make sure you know what you are doing before you undertake such changes.

SQLite 使用特殊SQLITE_SEQUENCE跟踪一个表曾经拥有的最大 ROWID 。SQLITE_SEQUENCE每当创建包含 AUTOINCREMENT 列的普通表时,就会自动创建和初始化该 表。可以使用普通的 UPDATE、INSERT 和 DELETE 语句修改 SQLITE_SEQUENCE 表的内容。但是对该表进行修改可能会干扰 AUTOINCREMENT 密钥生成算法。在进行此类更改之前,请确保您知道自己在做什么。

回答by Hu?nh Ng?c Bang

You can reset by update sequence after deleted rows in your-table

您可以在删除表中的行后按更新顺序重置

UPDATE SQLITE_SEQUENCE SET SEQ=0 WHERE NAME='table_name';

回答by Daniel Lefebvre

As an alternate option, if you have the Sqlite Database Browser and are more inclined to a GUI solution, you can edit the sqlite_sequence table where field name is the name of your table. Double click the cell for the field seq and change the value to 0 in the dialogue box that pops up.

作为替代选项,如果您有 Sqlite 数据库浏览器并且更倾向于 GUI 解决方案,您可以编辑 sqlite_sequence 表,其中字段名称是表的名称。双击字段 seq 的单元格,在弹出的对话框中将值更改为 0。

回答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);
}