Skip to content Skip to sidebar Skip to footer

Resetting Autoincrement In Android Sqlite

I have this method which will remove all rows from a table but I also want it to reset the autoincrement so that when a new row is added it will start again. The SQL statement I'm

Solution 1:

you'll need single quotes around your table name, i.e.

db.execSQL("DELETE FROM SQLITE_SEQUENCE WHERE NAME = '" + TABLE_NAME + "'");

Solution 2:

Building on dldnh's answer:

The following query will set seq to the largest value in the col identity column in the Tbl table, so there is no risk of violating constraints.

UPDATE sqlite_sequence SET seq = (SELECTMAX(col) FROM Tbl) WHERE name="Tbl"

Solution 3:

I was trying too hard.

Finally, I got this answer code...

Book.deleteAll(Book.class);
book=new Book(user, pass);
book.setId(Long.valueOf(book.listAll(Book.class).size()));
book.save();

this code work like delete and recreating the table.and reset id.

good luck

Solution 4:

you can also use delete() method of SQLiteDatabase, like this

db.delete("SQLITE_SEQUENCE","NAME = ?",new String[]{TABLE_NAME});

Post a Comment for "Resetting Autoincrement In Android Sqlite"