会议室数据库版本问题

时间:2020-05-21 22:24:09

标签: java android version android-room

在我的android应用程序中。我正在使用retorfit获取一些数据服务器,然后将该数据存储在会议室数据库中。但是我提出了一个逻辑,即每次启动时都会在启动屏幕上启动。它清除整个表,然后再次进行网络呼叫和清空表格。我再次插入数据。 5次中有1次出现版本问题,请增加版本 在创建活动时,我运行此代码。我删除数据库

SplashScreen.this.getApplicationContext().deleteDatabase("alampa&disma");
categoryTableAbstarctClass=Room.databaseBuilder(getApplicationContext(),CategoryTableAbstarctClass.class,"alampa&disma").fallbackToDestructiveMigration().allowMainThreadQueries().build();

然后,当响应成功时,我进行网络呼叫。这就是我要做的

if(response.body().getCategories().size()>0)
                                {
                                    categoryTableAbstarctClass.categoryTableDataAccess().deleteCategories(); // Here I am deleting the database
                                    for (int i = 0; i < response.body().getCategories().size(); i++) {

                                        CategoryTable categoryTable = new CategoryTable();
                                        categoryTable.setId(Integer.parseInt(response.body().getCategories().get(i).getId()));


                                        categoryTable.setCategoryID(response.body().getCategories().get(i).getCid());
                                        categoryTable.setCategoryName(response.body().getCategories().get(i).getName());
                                        categoryTable.setCategoryPic(response.body().getCategories().get(i).getPic());
                                        categoryTable.setCategoryDescription(response.body().getCategories().get(i).getDescription());
                                        categoryTable.setCategoryPostingDate(response.body().getCategories().get(i).getPostingDate());
                                        categoryTable.setCategoryUpdationDate(response.body().getCategories().get(i).getUpdationDate());

                                        categoryTableAbstarctClass.categoryTableDataAccess().addCategories(categoryTable);

                                    }
                                }

这是我的abstarct类的代码

@Database(entities = {CategoryTable.class},version = 2, exportSchema = false)
public abstract class CategoryTableAbstarctClass extends RoomDatabase {
public abstract CategoryTableDataAccess categoryTableDataAccess();
}

这些是我的查询

@Insert
public void addCategories(CategoryTable categoryTable);

@Query("SELECT * FROM category")
public List<CategoryTable> fetchCategories();

@Query("SELECT * FROM category WHERE Category_ID = :StringCategoryID")
public List<CategoryTable> fetchCategoriesByCategoryID(String StringCategoryID);


@Query("DELETE FROM category")
public void deleteCategories();

当我删除表并重新创建它为什么会引起版本控制问题时,我想找出解决方案

0 个答案:

没有答案