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 using isnt working due to certain columns not existing. Am i doing it right?

private void rmvall() {

    SQLiteDatabase db = appts.getWritableDatabase();
    db.delete(TABLE_NAME, null, null);
    db.execSQL("DELETE FROM SQLITE_SEQUENCE WHERE NAME = " + TABLE_NAME);

}

  • FacebookSdk.sdkInitialize(getApplicationContext()) Deprecated
  • Cordova and setting Android using command line
  • Getting the pixel color value of a point on an Android View that includes a Bitmap-backed Canvas
  • Trying to build a correct SOAP Request
  • Cannot run program “\ndk-build.cmd”: Launching failed
  • rotation and scaling using multi touch in android
  • Related posts:

    How to create custom button in Android using XML Styles
    Content-Length header already present
    Android: How to automatically generate Java code from layout file?
    Can't find android device using “adb devices” command
    Code completion to in Android Studio
    Complex ListView example with complex data and complex layout of each row?
  • Creating RTP Packets from Android Camera to Send
  • Send Post Request with params using Retrofit
  • Why is the Android emulator so slow? How can we speed up the Android emulator?
  • Error:(26, 0) Gradle DSL method not found: 'runProguard()'
  • How to get selected item of a singlechoice Alert Dialog?
  • SyncAdapter alternatives
  • 4 Solutions collect form web for “Resetting Autoincrement in Android SQLite”

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

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

    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 = (SELECT MAX(col) FROM Tbl) WHERE name="Tbl"
    

    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

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

    db.delete("SQLITE_SEQUENCE","NAME = ?",new String[]{TABLE_NAME});
    
    Android Babe is a Google Android Fan, All about Android Phones, Android Wear, Android Dev and Android Games Apps and so on.