如何在sqlite中重置自动增量序列号
如何在 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 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(5)
基于 Marcos Vasconcelos 的答案:
此查询会将
seq
设置为 <Tbl
表中的 code>col 标识列,因此不存在违反约束的风险。Building on Marcos Vasconcelos' answer:
This query will set
seq
to the largest value in thecol
identity column in theTbl
table, so there is no risk of violating constraints.在您的 .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 tableseq
a integer indicating the current last value at this tableYou can update it to
0
But beware if your table use this id as the unique identifier.
如果要在 ORMLite 中发出通用数据库命令,可以使用 updateRaw 方法。请参阅 javadocs。还有用于其他命令的
executeRaw
。您也可以删除并重新创建该表:
我认为真正的问题是为什么您的应用程序取决于此数据库内部编号?确实不应该在意。
Math
仍将位于 id #1。If you want to issue general database commands in ORMLite, you can use the
updateRaw
method. See the javadocs. There is alsoexecuteRaw
for other commands.You could also drop and recreate the table as well:
I think the real question is why is your application depending on this database internal number? It really shouldn't care.
Math
would still be at id #1.这在我的数据库中对我有用:(我将id设置在1之前,然后删除一行后,当我再次添加新数据行时,自动增量序列保持正常):
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 ) :