samedi 17 octobre 2015

Reset INTEGER PRIMARY KEY AUTOINCREMENT in Android

I created a table called DRINK. I inserted two rows in to it initially. When upgraded, I would like to delete both rows and reset my Primary Key to 1. I am getting the rows to delete but am not having any luck resetting the primary key. Does anybody know the syntax on how to do that or if it's even possible? Here's the function which is called from my onUpdate function:

    private void updateMyDatabase(SQLiteDatabase db, int olderVersion, int newVersion){
    Log.v("DatabaseHelper","UPDATE MY DATABASE");
    if (olderVersion<1){
        db.execSQL("CREATE TABLE DRINK (_id INTEGER PRIMARY KEY AUTOINCREMENT,"
                + "NAME TEXT, "
                + "DESCRIPTION TEXT, "
                + "IMAGE_RESOURCE_ID INTEGER);");
        insertDrink(db,"latte","Espresso and Steamed Milk",R.drawable.latte);
        insertDrink(db, "cappuccino", "This a Cappuccino", R.drawable.cap);
    }
    if (olderVersion < 2){
        db.execSQL("delete from DRINK");
        db.execSQL("alter table AUTO_INCREMENT =1"); //*** NEED HELP HERE***
        insertDrink(db, "coff", "Espresso and Steamed Milk", R.drawable.latte);
    }
}

I found this piece of code online but it's not working. Thank you.

db.execSQL("alter table AUTO_INCREMENT =1");

Aucun commentaire:

Enregistrer un commentaire