I have a question about deleteAll() function. I’m using a key with autoincrement property for my database. When I delete all rows from my db, it deletes everything. But when I insert a new row, key doesn’t start from 1, for example before deletion key was 15, new key becomes 16 instead of 1 after deletion. How can I fix this? Any suggestions?
here my codes
public int deleteAll(){
return sqLiteDatabase.delete(MYDATABASE_TABLE, null, null);
}
public void onCreate(SQLiteDatabase db) {
db.execSQL(SCRIPT_CREATE_DATABASE);
}
And my database script
private static final String SCRIPT_CREATE_DATABASE =
"create table "
+ MYDATABASE_TABLE + "("
+ KEY_ID + " integer primary key autoincrement, "
+ COLUMN_NAME + " text not null, "
+ COLUMN_EMAIL + " text not null, "
+ COLUMN_PHONE + " text not null, "
+ COLUMN_ADDRESS + " text not null"
+ ");";
Your code shouldn’t depend on the value of the auto-incremented key.
For displaying to the user, use a separate counter when you iterate the database cursor. Users generally don’t need to know the key, but just a serial number. Both of them can be different.
But if you do insist on restarting the key value, try dropping the table and recreating it. But beware of the old key values being used in other tables as foreign keys. Use triggers to handle such cases.