Android的SQLite的自动递增Android、SQLite

2023-09-05 05:33:39 作者:Moonlight(月光)

我现在有一个表称为用户有它为

I currently have a table called User which has a id column which is created as

INTEGER PRIMARY KEY

'INTEGER PRIMARY KEY'

可以说,我已经创建了两个用户,这样的表有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更大的至少一个   以往任何时候都存在于同一个表。

If a column has the type INTEGER PRIMARY KEY AUTOINCREMENT then... the ROWID chosen for the new row is at least one larger than the largest ROWID that has ever before existed in that same table.

在AUTOINCREMENT关键字实现的行为是巧妙地   不同于默认行为。包含自动增量,行,   自动选择的ROWID都保证具有的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.