恢复sqlite数据库后,旧文件保持活动状态?
我的应用程序中有一个恢复选项,它会读取备份文件,并将其复制到原始 sqlite-db 上。
一切似乎都工作正常,除了恢复后旧的数据库文件仍然处于活动状态之外。如果我使用 System.exit(0); 完全关闭应用程序,它会加载恢复的数据库,并且一切都会按预期进行。
似乎应用程序将旧的数据库文件保留在 RAM 中,尽管这看起来很奇怪......
代码下方:
File dbFile = new File(DatabaseHelper.DB_PATH + DatabaseHelper.DB_NAME);
if (dbFile.exists()) {
Boolean fileDeleted = dbFile.delete();
Log.i(DatabaseHelper.LOG_TAG, "fileDeleted: " + String.valueOf(fileDeleted));
}
try {
Log.i(DatabaseHelper.LOG_TAG, "myDatabase.isOpen: " + String.valueOf(dbHelper.myDataBase.isOpen()));
dbHelper.cleanup();
Log.i(DatabaseHelper.LOG_TAG, "myDatabase.isOpen: " + String.valueOf(dbHelper.myDataBase.isOpen()));
Boolean newFileCreated = dbFile.createNewFile();
Log.i(DatabaseHelper.LOG_TAG, "newFileCreated: " + String.valueOf(newFileCreated));
FileUtil.copyFile(dbBackupFile, dbFile);
return null;
} catch (IOException e) {
Log.e(DatabaseHelper.LOG_TAG, e.getMessage(), e);
return e.getMessage();
}
有人经历过同样的事情吗?
我确实检查了某些活动是否打开了数据库,但据我所知,情况并非如此。所有日志条目都显示预期/正确的值...
我已经尝试了一切,因此非常感谢任何建议/想法!
提前致谢。
问候, 科恩·霍雷尔贝克
I have a restore option in my app, which reads in the back-up file, and copies it over the original sqlite-db.
Everything seems to work fine, except for the fact that after the restore, the old db-file is still active. If I shut down my application completely, using System.exit(0);
, it does load the restored database, and everything works as expected.
It seems that the application keeps the old db-file in RAM, although that seems odd ...
Below the code:
File dbFile = new File(DatabaseHelper.DB_PATH + DatabaseHelper.DB_NAME);
if (dbFile.exists()) {
Boolean fileDeleted = dbFile.delete();
Log.i(DatabaseHelper.LOG_TAG, "fileDeleted: " + String.valueOf(fileDeleted));
}
try {
Log.i(DatabaseHelper.LOG_TAG, "myDatabase.isOpen: " + String.valueOf(dbHelper.myDataBase.isOpen()));
dbHelper.cleanup();
Log.i(DatabaseHelper.LOG_TAG, "myDatabase.isOpen: " + String.valueOf(dbHelper.myDataBase.isOpen()));
Boolean newFileCreated = dbFile.createNewFile();
Log.i(DatabaseHelper.LOG_TAG, "newFileCreated: " + String.valueOf(newFileCreated));
FileUtil.copyFile(dbBackupFile, dbFile);
return null;
} catch (IOException e) {
Log.e(DatabaseHelper.LOG_TAG, e.getMessage(), e);
return e.getMessage();
}
Somebody experienced the same?
I did check to see if some activity has the db open, but as far as I can see, this is nowhere the case. All the log-entries show expected/correct values ...
I've tried everything, so any suggestions/ideas are very much appreciated!!
Thanks in advance.
Greetingz,
Koen Hoorelbeke<
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(1)
为什么要使用
System.exit(0)
,请使用finish()
。这将finish()
活动,GC 将处理内存,您将向解决方案迈出一步。我认为文件被删除需要一些时间,我在模拟器上注意到了它。您是否从任何文件夹(原始/资产)复制数据库?我建议您在复制旧数据库和新数据库之前尝试
close()
或releaseMemory()
数据库。然后将新的数据库文件重新分配给db对象Why you are using
System.exit(0)
, usefinish()
. Which willfinish()
the activity and GC will take care of the memory, and you'll be a step forward to solution. I think it takes some time before the file is deleted, I've notice it on the emulator. Are you copying the database from any folder (raw/assets)?I suggest you to try
close()
orreleaseMemory()
the database before you do the copying stuff of the old one and the new one. And then reassign to the db object the new database file