Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Android SQLite auto increment

Tags:

android

sqlite

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 1 and 2

If I delete the second user and create a third the id is 2, I need this to be 3

So it seems Android is selecting the next available id, how can I change this to its more like a sequence number?

Regards

like image 753
user964283 Avatar asked Dec 08 '11 17:12

user964283


People also ask

Does SQLite have auto increment?

SQLite AUTOINCREMENT is a keyword used for auto incrementing a value of a field in the table. We can auto increment a field value by using AUTOINCREMENT keyword when creating a table with specific column name to auto increment. The keyword AUTOINCREMENT can be used with INTEGER field only.

How does auto increment work in SQLite?

AUTOINCREMENT guarantees that automatically chosen ROWIDs will be increasing but not that they will be sequential. Because AUTOINCREMENT keyword changes the behavior of the ROWID selection algorithm, AUTOINCREMENT is not allowed on WITHOUT ROWID tables or on any table column other than INTEGER PRIMARY KEY.

When you insert a new row into a SQLite database it automatically generates a value for?

If you don't specify the rowid value or you use a NULL value when you insert a new row, SQLite automatically assigns the next sequential integer, which is one larger than the largest rowid in the table. The rowid value starts at 1.

How do I use wildcards in SQLite?

SQLite LIKE examples To find the tracks whose names start with the Wild literal string, you use the percent sign % wildcard at the end of the pattern. To find the tracks whose names end with Wild word, you use % wildcard at the beginning of the pattern.


1 Answers

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.

like image 65
Graham Borland Avatar answered Oct 12 '22 04:10

Graham Borland