Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

How do I completely recreate my database in Android Room?

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

I'm aware that I can get access to a SupportSQLiteOpenHelper via room and drop all the tables myself, but there doesn't seem to be a good way to kick-off the recreation process.

Also, I'm aware that I could delete every item from each table without dropping it, but that's not what I want. That doesn't reset the auto-incrementing primary key, so the "id" field of new items won't reset back to 1.

Thanks!

EDIT:
This is something I want to be able to do arbitrarily at runtime.

EDIT 2:
The method should be maintainable, i.e. not involve hand-writing SQL that matches Room's behavior. Ideally there would be some way to retrieve the SQL that Room generates, or a SQLiteOpenHelper.onCreate() equivalent method. Or anything else that solves this problem! :)

like image 849
Greyson Parrelli Avatar asked Jun 28 '17 18:06

Greyson Parrelli


People also ask

How do I clear my database room?

3.1 Add the Clear all data menu optionIn the Options menu, select Clear all data. All words should disappear. Restart the app. (Restart it from your device or the emulator; don't run it again from Android Studio) You should see the initial set of words.

How do I find the room database file on my Android phone?

Go to Tools -> DDMS or click the Device File Explorer below Layout Preview in right bar. Device Monitor window will open. In File Explorer tab, click data -> data -> your project name -> databases . Click pull a file from device icon.

What is Android room database?

What is a Room database? Room is a database layer on top of an SQLite database. Room takes care of mundane tasks that you used to handle with an SQLiteOpenHelper . Room uses the DAO to issue queries to its database. By default, to avoid poor UI performance, Room doesn't allow you to issue queries on the main thread.


2 Answers

I found it easiest to do this via context.deleteDatabase(“name”) and then simply reinstantiating and repopulating the database via the Room.databaseBuilder().addCallback upon first access.

like image 173
Bink Avatar answered Oct 14 '22 18:10

Bink


Simple answer is to increment your @Database version number. Generally speaking you should do this for schema changes, however it will achieve your aims 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: IF you want to do this at run time, I would clear all the data from your tables (to avoid FK issues), then call DROP TABLE table_name, on all of your respective tables. Then you will need to write queries for creating the tables ie : CREATE TABLE table_name (uid INTEGER PRIMARY KEY, name STRING);.

This will mean you have to maintain a list of create table queries up to date with your POJO's unfortunatly. Ideally you'd be able to use reflection to generate the query, howerever @ColumnInfo currently doesn't support reflection as it has its RetentionPolicy set to CLASS.

Hopefully this solves your problem, feel free to ask for further clarification in the comments. Happy hunting!

like image 28
Jack Dalton Avatar answered Oct 14 '22 17:10

Jack Dalton