SQLite中删除行之后,一个新条目_ID不连续, [英] After deleting a row in SQLite, _ID of a new entry is not consecutive

查看:1360
本文介绍了SQLite中删除行之后,一个新条目_ID不连续,的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我用这个方法在我的SQLite数据库删除行:

I use this method to delete a row in my sqlite db:

db.execSQL("delete from "+TABLE_NUMS+" where _ID = '" + this.rowID + "'");

然后我更新ID的休息让我的作品连续:

and then I update the rest of Ids to make my entries consecutive:

db.execSQL("UPDATE "+TABLE_NUMS+" set _ID = (_ID - 1) WHERE _ID > "+this.rowID);

和它工作正常,但是当我添加新条目到我的数据库,新的项目的ID仍然增加,如果被删除的条目存在,说我有10行与开始从1到10的ID,然后我删除5号和6,行成了1到8,但新进入的ID将是11.所以,我的ID序列是1至8和11我该如何解决这个问题?

And it works fine, but when I add new entries to my DB, the ID of the new entries still add as if the deleted entries existed, say I have 10 rows with IDs starting from 1 to 10, and then I delete number 5 and 6, the rows become 1 to 8, but the new entry's ID will be 11. So my IDs sequence would be 1 to 8 and 11. How can I fix this?

推荐答案

SQLite的跟踪最大的ROWID的一个表已经使用特殊SQLITE_SEQUENCE表有史以来举办。您可修改该序列为:

SQLite keeps track of the largest ROWID that a table has ever held using the special SQLITE_SEQUENCE table. You cam modify that sequence as:

UPDATE SQLITE_SEQUENCE SET seq = this.ID -1 WHERE name = TABLE_NUMS 

同样的功能被问这个问题

这篇关于SQLite中删除行之后,一个新条目_ID不连续,的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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