问题描述
我目前有一个名为 User 的表,它有一个 id 列,创建为
I currently have a table called User which has a id column which is created as
'整数主键'
假设我创建了两个用户,所以表的 ID 为 1 和 2
Lets say I have created two users so the table has id 1 and 2
如果我删除第二个用户并创建第三个用户 ID 为 2,我需要将其设为 3
If I delete the second user and create a third the id is 2, I need this to be 3
所以看起来 Android 正在选择下一个可用的 ID,我该如何将其更改为更像是一个序列号?
So it seems Android is selecting the next available id, how can I change this to its more like a sequence number?
问候
推荐答案
使其INTEGER PRIMARY KEY AUTOINCREMENT NOT NULL
.这是文档所说的:
如果一列的类型为 INTEGER PRIMARY KEY AUTOINCREMENT 那么...选择的 ROWID因为新行至少比最大的 ROWID 大 1曾经存在于同一张表中.
AUTOINCREMENT 关键字实现的行为是巧妙的与默认行为不同.使用 AUTOINCREMENT,行自动选择的 ROWID 保证具有具有以前从未被同一个数据库中的同一个表使用过.而自动生成的 ROWID 保证是单调的增加.
The behavior implemented by the AUTOINCREMENT keyword is subtly different from the default behavior. With AUTOINCREMENT, rows with automatically selected ROWIDs are guaranteed to have ROWIDs that have never been used before by the same table in the same database. And the automatically generated ROWIDs are guaranteed to be monotonically increasing.
这篇关于Android SQLite 自动增量的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持!