如何在sqlite中重置自动增量序列号

发布于 2024-10-31 02:01:30 字数 1499 浏览 1 评论 0原文

如何在 Ormlite 中更新表 sqlite_sequence ?我只需要更新序列。我如何通过 ORMLite 获取该表?

编辑

我找不到 ORLite 工具来执行此操作,因此我使用简单的 sqlite 查询。在我的类扩展 OrmLiteSqliteOpenHelper 中,我使用 SQLiteDatabase 进行更新。

EDIT2 ;)

在我的项目中,我保留了 Lesson 类和 WeekDefinition 类。

class Lesson{
    @DatabaseField(generatedId=true)
    private int id;
    ...
}

class WeekDefinitions{
    @DatabaseField(generatedId=true)
    private int id;
    @DatabaseField(foreign=true, columnName="lesson_id")
    private Lesson lesson;
    ...
}

现在,当我添加新课程时,id 会递增。例如

id = 1 Math
id = 2 English
id = 3 Medicine

,在 weekDefinition 中:

id = 1 lesson_id = 1  nr = 20
id = 2 lesson_id = 1  nr = 22
id = 3 lesson_id = 2  nr = 32
...
id = 12 lesson_id = 3  nr = 3

SQLite 将此行添加到 sqlite_sequence 中(当使用自动增量时)

rowId = 1   name = lesson         seq = 3
rowId = 2   name = weekDefinition seq = 12

现在,我从表 Lesson 和 WeekDefinition 中删除所有行。之后Lesson和WeekDef为空,但sqlite_sequence仍然相同。这是问题,因为表课程中的 id 从值 4 开始(课程来自 sqlite_sequence 的 seq 并添加 1 ):

id = 4 Math
id = 5 English
id = 6 Medicine

并且 weekDefinition

id = 13 lesson_id = 1  nr = 20
id = 14 lesson_id = 1  nr = 22
id = 15 lesson_id = 2  nr = 32

和课程 id = 4 ,数学我应该得到 weekDefinitios,但在 weekDefinitions Lesson_id 中,其值仅从 1 到 3 这是我的问题。我需要“重置”sqlite_sequence 表(或者有更好的解决方案?)

How to update table sqlite_sequence in Ormlite ? I just need update seq. How can I get that table via ORMLite ?

EDIT

I can't find ORLite tool to do this, so instead I use simple sqlite query. In my class extends OrmLiteSqliteOpenHelper I use SQLiteDatabase to make that update.

EDIT2 ;)

In my project I persist class Lesson and class WeekDefinition.

class Lesson{
    @DatabaseField(generatedId=true)
    private int id;
    ...
}

class WeekDefinitions{
    @DatabaseField(generatedId=true)
    private int id;
    @DatabaseField(foreign=true, columnName="lesson_id")
    private Lesson lesson;
    ...
}

Now , when I add new lessons, id is increment. For example

id = 1 Math
id = 2 English
id = 3 Medicine

and in weekDefinition :

id = 1 lesson_id = 1  nr = 20
id = 2 lesson_id = 1  nr = 22
id = 3 lesson_id = 2  nr = 32
...
id = 12 lesson_id = 3  nr = 3

SQLite add this row into sqlite_sequence ( when use autoincrement )

rowId = 1   name = lesson         seq = 3
rowId = 2   name = weekDefinition seq = 12

Now, I delete all rows from tables Lesson and WeekDefinition. Lesson and WeekDef are empty after that, but sqlite_sequence is still the same. And this is problem because id in table lesson start from value 4 ( seq from sqlite_sequence for lesson and add 1 ) :

id = 4 Math
id = 5 English
id = 6 Medicine

and weekDefinition

id = 13 lesson_id = 1  nr = 20
id = 14 lesson_id = 1  nr = 22
id = 15 lesson_id = 2  nr = 32

and for lesson id = 4 , Math i should get weekDefinitios, but in weekDefinitions lessons_id has value only from 1 to 3
And this is my problem. I need "reset" sqlite_sequence table ( or there is better solution ?)

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(5

心安伴我暖 2024-11-07 02:01:30

基于 Marcos Vasconcelos 的答案

UPDATE sqlite_sequence SET seq = (SELECT MAX(col) FROM Tbl) WHERE name="Tbl"

此查询会将 seq 设置为 < Tbl 表中的 code>col 标识列,因此不存在违反约束的风险。

Building on Marcos Vasconcelos' answer:

UPDATE sqlite_sequence SET seq = (SELECT MAX(col) FROM Tbl) WHERE name="Tbl"

This query will set seq to the largest value in the col identity column in the Tbl table, so there is no risk of violating constraints.

蓝颜夕 2024-11-07 02:01:30

在您的 .db 文件中,有一个名为 sqlite_sequence 的表,

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

您可以将其更新为 0

但请注意,如果您的表使用此 id 作为唯一标识符。

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.

不知所踪 2024-11-07 02:01:30
UPDATE SQLITE_SEQUENCE SET SEQ= 'value' WHERE NAME='table_name';
UPDATE SQLITE_SEQUENCE SET SEQ= 'value' WHERE NAME='table_name';
忱杏 2024-11-07 02:01:30

如果要在 ORMLite 中发出通用数据库命令,可以使用 updateRaw 方法。请参阅 javadocs。还有用于其他命令的executeRaw

lessonDao.updateRaw("delete from 'lesson';");
lessonDao.updateRaw("delete from sqlite_sequence where name='lesson';");
weekDefinitionDao.updateRaw("delete from 'weekdefinition';");
weekDefinitionDao.updateRaw(
    "delete from sqlite_sequence where name='weekdefinition';");

您也可以删除并重新创建该表:

TableUtils.dropTable(WeekDefinition.class);
TableUtils.dropTable(Lesson.class);
TableUtils.createTable(Lesson.class);
TableUtils.createTable(WeekDefinition.class);

我认为真正的问题是为什么您的应用程序取决于此数据库内部编号?确实不应该在意。

  • 完全不显示该数字怎么样,这样它可以是 1 或 1001,这样您的应用程序就无关紧要了?
  • 您也可以永远不删除课程,但可以添加一个隐藏的布尔字段。因此,如果重新添加它们,隐藏字段可能会设置为 false,而 Math 仍将位于 id #1。

If you want to issue general database commands in ORMLite, you can use the updateRaw method. See the javadocs. There is also executeRaw for other commands.

lessonDao.updateRaw("delete from 'lesson';");
lessonDao.updateRaw("delete from sqlite_sequence where name='lesson';");
weekDefinitionDao.updateRaw("delete from 'weekdefinition';");
weekDefinitionDao.updateRaw(
    "delete from sqlite_sequence where name='weekdefinition';");

You could also drop and recreate the table as well:

TableUtils.dropTable(WeekDefinition.class);
TableUtils.dropTable(Lesson.class);
TableUtils.createTable(Lesson.class);
TableUtils.createTable(WeekDefinition.class);

I think the real question is why is your application depending on this database internal number? It really shouldn't care.

  • How about not displaying the number at all so it can be 1 or 1001 and your application won't matter?
  • You could also never remove the lessons at all but maybe add a hidden boolean field. So if they get re-added, the hidden field could be set to false and Math would still be at id #1.
请帮我爱他 2024-11-07 02:01:30

这在我的数据库中对我有用:(我将id设置在1之前,然后删除一行后,当我再次添加新数据行时,自动增量序列保持正常):

 public void  updateSerialNumber ( long memberId){
        String query = "UPDATE SQLITE_SEQUENCE SET SEQ= '"+(memberId-1)+"' WHERE NAME='"+ReportBigHelper.TABLE_MEMBER+"'";
        database.execSQL(query);
    }

This worked for me in my database : (I Set the id before one, then after deleting one row, when I add new data row again, the auto increment serial remain ok ) :

 public void  updateSerialNumber ( long memberId){
        String query = "UPDATE SQLITE_SEQUENCE SET SEQ= '"+(memberId-1)+"' WHERE NAME='"+ReportBigHelper.TABLE_MEMBER+"'";
        database.execSQL(query);
    }
~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文