我怎么能重置sqlite的一个自动递增序列号序列号、我怎么能、sqlite

2023-09-04 07:28:08 作者:在飘雪的季节守候花开

如何更新Ormlite表sqlite_sequence?我只是需要更新序列。我怎样才能获得该表通过ORMLite?

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

修改

我找不到ORLite工具来做到这一点,所以不是我用简单的SQLite查询。在我的课堂延伸OrmLiteSqliteOpenHelper我使用SQLiteDatabase使这一更新。

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

在我的项目中,我坚持一流的课程和班级WeekDefinition。

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;
    ...
}

现在,当我添加新的经验教训,id是增量。例如

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

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

和在weekDefinition:

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的添加此行到sqlite_sequence(当使用自动增量)

SQLite add this row into sqlite_sequence ( when use autoincrement )

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

现在,我删除表的课程和WeekDefinition所有行。课程和WeekDef是后空,但sqlite_sequence还是一样。这是问题,因为ID表教训值4开始(SEQ从sqlite_sequence的教训,并添加1):

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

和weekDefinition

and 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 lessons_id具有值仅1至3个 这是我的问题。我需要复位sqlite_sequence表(或者有更好的解决办法?)

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

推荐答案

建立在马科斯德瓦斯康塞洛斯的回答:

Building on Marcos Vasconcelos' answer:

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

此查询将设置序列 COL 标识列在 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.