John Joe
John Joe

Reputation: 12803

SQLite id auto-increment

I have created a SQLite table by using query below

 db.execSQL("create table " + TABLE__WORK + " ( " + ID1 + " INTEGER PRIMARY KEY ,Name TEXT, Title TEXT, Time TEXT, Date TEXT)");

The ID now can be auto-incremented. But after I perform delete query, delete the first row data and add again, the ID will start from 1 instead of 2. Is it possible to make the id become 2 ?

Upvotes: 1

Views: 17968

Answers (2)

Priyank Patel
Priyank Patel

Reputation: 12382

Make it INTEGER PRIMARY KEY AUTOINCREMENT NOT NULL.

Here's what the SQLite Autoincrement document say:

If the AUTOINCREMENT keyword appears after INTEGER PRIMARY KEY, that changes the automatic ROWID assignment algorithm to prevent the reuse of ROWIDs over the lifetime of the database. In other words, the purpose of AUTOINCREMENT is to prevent the reuse of ROWIDs from previously deleted rows.

Upvotes: 3

Mihai Alexandru-Ionut
Mihai Alexandru-Ionut

Reputation: 48327

SQLite AUTOINCREMENT is a keyword used for auto incrementing a value of a field in the table.You do not need ID1.

See reference here

Please use this:

db.execSQL("create table " + TABLE__WORK + " (ID INTEGER PRIMARY KEY AUTOINCREMENT NOT NULL,Name TEXT, Title TEXT, Time TEXT, Date TEXT)");

Upvotes: 8

Related Questions