恢复sqlite数据库后,旧文件保持活动状态?

发布于 2024-11-26 23:48:30 字数 1373 浏览 1 评论 0原文

我的应用程序中有一个恢复选项,它会读取备份文件,并将其复制到原始 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 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(1

傲影 2024-12-03 23:48:30

为什么要使用System.exit(0),请使用finish()。这将 finish() 活动,GC 将处理内存,您将向解决方案迈出一步。我认为文件被删除需要一些时间,我在模拟器上注意到了它。您是否从任何文件夹(原始/资产)复制数据库?

我建议您在复制旧数据库和新数据库之前尝试 close()releaseMemory() 数据库。然后将新的数据库文件重新分配给db对象

Why you are using System.exit(0), use finish(). Which will finish() 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

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文