Search code examples
androidandroid-sqliteandroid-room

Android room - clearing sqlite_sequence is not working


Following code is not resetting/clearing the auto increment id to 0

database = Room.databaseBuilder(getApplicationContext(), AppDatabase.class, DatabaseMeta.DB_NAME)
            .build();
database.query("DELETE FROM sqlite_sequence WHERE name = ?", new Object[]{"tableName"});

or

database.query("REPLACE INTO sqlite_sequence (name, seq) VALUES ('mission', -1)", null);

Is there anyway to clear sqlite_sequence in Android room?

Android room is not using "sqlite_sequence" for auto increment logic i think, not sure. Tried printing sqlite_sequence entries, can't find the table name i used(which has the auto increment primary key).


Solution

  • Try this code works for me

    class FeedReaderDbHelper extends SQLiteOpenHelper {
        static final int DATABASE_VERSION = 1;
        static final String DATABASE_NAME = DBConfig.DATABASE_NAME;
    
        FeedReaderDbHelper(Context context) {
            super(context, DATABASE_NAME, null, DATABASE_VERSION);
        }
    
        public void onCreate(SQLiteDatabase db) {
        }
    
        public void onUpgrade(SQLiteDatabase db, int oldVersion, int newVersion) {
            onCreate(db);
        }
    
        public void onDowngrade(SQLiteDatabase db, int oldVersion, int newVersion) {
            onUpgrade(db, oldVersion, newVersion);
        }
    }
    
    FeedReaderDbHelper mDbHelper = new FeedReaderDbHelper(mainContext.getApplicationContext());
    mDbHelper.getWritableDatabase().execSQL("DELETE FROM sqlite_sequence WHERE name='table_name';");