重置Android Room库中的自动增量 [英] Reset auto-increment in Android's Room library

查看:225
本文介绍了重置Android Room库中的自动增量的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我使用Android的Room库创建了一个表,该表存储了User类型的项目.对于ID列,该值是通过添加注释自动生成的

I created a table using Android's Room library, which stores items of type User. For the ID column, the value is automatically generated by adding the annotation

@PrimaryKey(autoGenerate = true)

在下一个应用程序运行中,我删除表中的所有条目.但是,自动生成的值不会重置为0,而是会在上一次运行的左侧继续.

In the next application run, I delete all entries in the table. However, the auto-generated value is not reset to 0, but continues where it left of the previous run.

删除表中的所有条目时如何重置自动递增计数器?

How can I reset the auto-increment counter when I delete all entries in a table?

推荐答案

您为什么要问这个更相关的问题.具有AUTOINCREMENT的列将是 rowid 列的别名,该列用于唯一标识行.除了识别行之外,最好不要依靠此值.

Why would you, is perhaps the more pertinent question. A column with AUTOINCREMENT will be an alias of the rowid column which is used to uniquely identify a row. It isn't the best practice to rely upon this value other than for identifying a row.

使用AUTOINCREMENT(autogenerate = true)会创建一个内部表 sqlite_sequence ,该表中的一行包含有史以来使用量最高的 rowid 的值,下一个 rowid 将是该值+1.因此是您的问题.

Using AUTOINCREMENT (autogenerate = true) results in an internal table sqlite_sequence being created, a row in that table holds the value of the highest ever used rowid The next rowid will be that values + 1. Hence your issue.

要从1重新开始,您必须将 sqlite_sequence 表中的相应行更新为0,或删除 sqlite_sequence 表中的相应行.

As such to restart from 1 you would have to either update the respective row in the sqlite_sequence table to 0 or delete the respective row in the sqlite_sequence table.

您不妨查看

You may wish to have a look at Android Room - reset auto generated key on each app run. Noting that this could be the basis for resetting the sequence value, but that it will always resets the sequence value.

这篇关于重置Android Room库中的自动增量的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆