Ceri Westcott
Ceri Westcott

Reputation: 59

Sqlite Autoincrement

I'm trying to update the id column in a table of memos. So if a memo with the id of 3 gets deleted, the row underneath now becomes id of 3.

sqLiteDatabase.execSQL("CREATE TABLE " + TABLE_NAME + "(" +
            "ID INTEGER PRIMARY KEY AUTOINCREMENT, MEMO TEXT, URGENCY TEXT)");

to create the table, works perfectly fine but i'm getting a sqlite error "near autoincrement".

 db.execSQL("ALTER TABLE " + TABLE_NAME + " AUTOINCREMENT = 
                      (SELECT MAX(ID) FROM " + TABLE_NAME + ");");

Upvotes: 0

Views: 136

Answers (1)

Diego Torres Milano
Diego Torres Milano

Reputation: 69396

AUTOINCREMENT does not work as I guess you are expecting.

If a column has the type INTEGER PRIMARY KEY AUTOINCREMENT then a slightly different ROWID selection algorithm is used. 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.

Upvotes: 2

Related Questions