如何解决Android如何备份会议室数据库
我正在尝试了解Google如何备份Room数据库的数据。根据文档,它应该将内容保存在getDatabasePath()中,并且我的数据库在那里。当前,每次我卸载我的App时,房间数据库都会被清除(使用模拟器登录并在设置中备份)。我的目标是备份数据,以便在用户重新安装时保留数据。我将其添加到清单中,没有任何改变:
android:allowBackup="true"
我看到其他人和我有相反的问题,不确定我在做什么错。我需要让用户在应用程序本身上使用其Google帐户登录才能使此功能正常工作吗?也许我在一起误会了。
数据库:
@Database(entities = {BudgetEntry.class},version = 1,exportSchema = false)
公共抽象类AppDatabase扩展了RoomDatabase {
private static AppDatabase INSTANCE;
public abstract BudgetDao budgetDao();
public static synchronized AppDatabase getDatabase(Context context) {
if (INSTANCE == null) {
INSTANCE =
Room.databaseBuilder(context.getApplicationContext(),AppDatabase.class,"budget_database")
.fallbackToDestructiveMigration()
.addCallback(roomCallback)
.build();
}
return INSTANCE;
}
private static RoomDatabase.Callback roomCallback = new RoomDatabase.Callback() {
@Override
public void onCreate(@NonNull SupportsqliteDatabase db) {
super.onCreate(db);
new PopulateDatabaseAsyncTask(INSTANCE).execute();
}
};
private static class PopulateDatabaseAsyncTask extends AsyncTask<Void,Void,Void> {
private BudgetDao budgetDao;
private PopulateDatabaseAsyncTask(AppDatabase appDatabase) {
budgetDao = appDatabase.budgetDao();
}
//Example Add
@Override
protected Void doInBackground(Void... voids) {
return null;
}
}
}
存储库:
public class BudgetRepository {
private BudgetDao budgetDao;
private LiveData<List<BudgetEntry>> allEntries;
public BudgetRepository(Application application) {
AppDatabase database = AppDatabase.getDatabase(application);
budgetDao = database.budgetDao();
allEntries = budgetDao.getAllEntries();
}
public void insert(BudgetEntry budgetEntry) {
new InsertBudgetAsyncTask(budgetDao).execute(budgetEntry);
}
public void delete(BudgetEntry budgetEntry) {
new DeleteBudgetAsyncTask(budgetDao).execute(budgetEntry);
}
public void deleteallEntries() {
new DeleteallEntriesAsyncTask(budgetDao).execute();
}
public LiveData<List<BudgetEntry>> getAllEntries() {
return allEntries;
}
public LiveData<Integer> getCount() {
return budgetDao.getCount();
}
private static class InsertBudgetAsyncTask extends AsyncTask<BudgetEntry,Void> {
private BudgetDao budgetDao;
private InsertBudgetAsyncTask(BudgetDao budgetDao) {
this.budgetDao = budgetDao;
}
@Override
protected Void doInBackground(BudgetEntry... budgetEntries) {
budgetDao.addBudgetData(budgetEntries[0]);
return null;
}
}
private static class DeleteBudgetAsyncTask extends AsyncTask<BudgetEntry,Void> {
private BudgetDao budgetDao;
private DeleteBudgetAsyncTask(BudgetDao budgetDao) {
this.budgetDao = budgetDao;
}
@Override
protected Void doInBackground(BudgetEntry... budgetEntries) {
budgetDao.delete(budgetEntries[0]);
return null;
}
}
private static class DeleteallEntriesAsyncTask extends AsyncTask<Void,Void> {
private BudgetDao budgetDao;
private DeleteallEntriesAsyncTask(BudgetDao budgetDao) {
this.budgetDao = budgetDao;
}
@Override
protected Void doInBackground(Void... voids) {
budgetDao.deleteallEntries();
return null;
}
}
}
感谢您的帮助!
版权声明:本文内容由互联网用户自发贡献,该文观点与技术仅代表作者本人。本站仅提供信息存储空间服务,不拥有所有权,不承担相关法律责任。如发现本站有涉嫌侵权/违法违规的内容, 请发送邮件至 dio@foxmail.com 举报,一经查实,本站将立刻删除。