如何删除Android Room数据库的文件? [英] How do I delete the file of an Android Room database?
问题描述
我已经实现了一个会议室数据库,该数据库使用 SQLiteAssetHelper 从资源文件分发a>首次启动该应用程序.
I have implemented a room database which is distributed from a resource file using SQLiteAssetHelper the first time the app is started.
数据库包含游戏状态数据,因此,如果玩家想从头开始,我想再次从资源中复制数据库文件并覆盖本地/内部"文件.
The database contains game status data so if the Player wants to start all over again, I want to copy again the database file from the resource and overwrite the "local/internal" file.
所以我的想法是删除内部db文件,以便SQLiteAssetHelper
将再次从资源中复制初始数据库.
So my idea was to delete the interal db-file so that SQLiteAssetHelper
will copy the Initial database from the resource again.
谢谢!
Kev
推荐答案
这是一个有效的示例:
public static void deleteDatabaseFile(Context context, String databaseName) {
File databases = new File(context.getApplicationInfo().dataDir + "/databases");
File db = new File(databases, databaseName);
if (db.delete())
System.out.println("Database deleted");
else
System.out.println("Failed to delete database");
File journal = new File(databases, databaseName + "-journal");
if (journal.exists()) {
if (journal.delete())
System.out.println("Database journal deleted");
else
System.out.println("Failed to delete database journal");
}
}
但是,老实说,我认为在运行时删除数据库既不安全也不可靠.您将必须确保没有任何东西在使用它,并且与数据库之间没有任何打开的连接.
but honestly I don't think it will be safe nor reliable to delete database in runtime. You would have to ensure nothing is using it and there aren't any open connections with the database.
这篇关于如何删除Android Room数据库的文件?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!