How to completely recreate my database in Android Room?

I have a situation where I want to hard reset my database using the Android Room. Using SQLiteOpenHelper, I could do this by writing a method to delete all tables and indexes, then manually call SQLiteOpenHelper.onCreate().

I know that I can access SupportSQLiteOpenHelperthrough the room and independently drop all the tables, but there seems to be no good way to start the rest process.

In addition, I know that I can delete every element from each table without dropping it, but that is not what I want. This will not reset the primary key automatically incrementing, so the "id" field of the new elements will not reset back to 1.

Thanks!

EDIT:
This is what I want to do arbitrarily at runtime.

EDIT 2:
This method should be supported, i.e. It does not require manual spelling of SQL that matches the behavior of the room. Ideally, there would be some way to get the SQL that Room generates, or an equivalent method SQLiteOpenHelper.onCreate(). Or anything else that solves this problem! :)

+6
source share
1 answer

The simple answer is to increase your @Database version number. Generally speaking, you must do this to change the schema, however it will achieve your goals of clearing the database and resetting all primary key values.

@Database(entities = { Hello.class }}, version = 1) // <- you want to increase version by 1
@TypeConverters({})
public abstract class AppDatabase extends RoomDatabase {
    public abstract HelloDao helloTextDao();
}

EDIT: , ( FK), DROP TABLE table_name . , ..: CREATE TABLE table_name (uid INTEGER PRIMARY KEY, name STRING);.

, POJO . , @ColumnInfo , RetentionPolicy, CLASS.

, , . !

+1

All Articles