简体   繁体   English

Android Room - 自动增加@database版本号?

[英]Android Room - auto increment @database version number?

During development my Room db schema is very volatile. 在开发期间,我的Room db模式非常不稳定。 Every time I do any change to the schema I need to update my version number, like this; 每次我对架构进行任何更改时,我都需要更新我的版本号,就像这样;

@Database(version = 27,
    entities = {MyClass.class})

public abstract class MyDatabase extends RoomDatabase

I am using fallbackToDestructiveMigration too; 我也在使用fallbackToDestructiveMigration;

Room.databaseBuilder(context.getApplicationContext(), AppDatabase.class, SystemStrings.ROOM_DATABASE_NAME)
                // allow queries on the main thread.
                // Don't do this on a real app! 
                .allowMainThreadQueries()
                .fallbackToDestructiveMigration()
                .build();

Is there any way to avoid updating the version number for each "little" change? 有没有办法避免更新每个“小”更改的版本号? As I said, things are quite volatile right now. 正如我所说,现在情况非常不稳定。

Deleteing the database would result in it being initialised/built each time the App is run. 删除数据库将导致每次运行应用程序时对其进行初始化/构建。

You could do this using :- 你可以这样做: -

context.getApplicationContext().deleteDatabase(SystemStrings.ROOM_DATABASE_NAME); //<<<< ADDED before building Database.
//Your existing code follows 
Room.databaseBuilder(context.getApplicationContext(), AppDatabase.class, SystemStrings.ROOM_DATABASE_NAME)
                // allow queries on the main thread.
                // Don't do this on a real app! 
                .allowMainThreadQueries()
                .fallbackToDestructiveMigration()
                .build();

Of course you would not have this in the real app 当然,你不会在真正的应用程序中有这个

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM