问题
GreenDAO supports DaoMaster.dropAllTables() and DaoMaster.createAllTables(). I'm looking for similar functionality in RoomDB. Does RoomDB supports this functionality ??
The use-case for this functionality is, when the user tries to login with new mobile number to my app, i want to clear the data of old login number by showing a warning dialog message and allow login with new number.
回答1:
Room supports dropping and creating tables only during a migration between schema versions.
You can gain access to the underlying SupportSQLiteDatabase
via RoomDatabase.getOpenHelper().getReadableDatabase(). With that, you can use execSQL() to execute SQL statements, include table dropping and creating.
But doing so is not consistent with the intended use of Room and is not safe. It will very likely break the InvalidationTracker used to notify observers of changes to managed tables.
From your use-case, it seems like rather than dropping and recreating the tables, all you need to do is clear them by deleting all entries.
来源:https://stackoverflow.com/questions/62405921/does-roomdb-supports-dropalltables-and-createalltables