Android SQLite auto increment

后端 未结 4 1788
旧时难觅i
旧时难觅i 2020-12-01 05:55

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

\'INTEGER PRIMARY KEY\'

Lets say I have created two users so the table has id

4条回答
  •  天涯浪人
    2020-12-01 06:34

    Make it INTEGER PRIMARY KEY AUTOINCREMENT NOT NULL. Here's what the docs say:

    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.

    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.

提交回复
热议问题