如何重置房间数据库
How to reset room database
我尝试了很多方法来重置它。
allowBackup
和fullBackupOnly
已设置为 false。
.fallbackToDestructiveMigration()
并直接删除数据库和缓存文件。
但是没用。
你可以使用这个clear all tables
这会删除作为 Database.entities().
注册到此数据库的所有表中的所有行
最简单的方法是卸载应用程序,这会删除数据库文件。所以重新运行从一个全新的数据库开始。
要使用 .fallbackToDestructiveMigration()
,您必须通过增加版本号来调用迁移,但不能针对特定路径进行迁移。您可能会争辩说这不会重置数据库,因为新创建的数据库将具有更高的版本号。
使用 clearAllTables
不会完全重置数据库,因为它不会删除系统 table。最值得注意的是,sqlite_sequence,这是一个 table,它在每个 table 的基础上保存最新 rowid 的值。也就是说,如果您在 @PrimaryKey 注释中有 autogenerate = true
用于解析为 INTEGER 的列类型亲和性的 field/column,那么将对 AUTOINCREMENT 进行编码,然后将创建 sqlite_sequence table (如果不存在)并存储所述主键的最新(因此也是最高)值。因此,如果您已将 100 行(例如)插入到这样的 table 中,那么在 clearAllTables 之后,这 100 行仍将存储在 sqlite_sequnce table.
中
您也可以在构建数据库之前删除数据库。这是一个允许在构建时删除它的示例:-
@Database(entities = [Customer::class], version = 1)
abstract class CustomerDatabase: RoomDatabase() {
abstract fun customerDao(): CustomerDao
companion object {
private var instance: CustomerDatabase?=null
fun getDatabase(context: Context, resetDatabase: Boolean): CustomerDatabase {
if (resetDatabase && instance == null) {
(context.getDatabasePath("thedatabase.db")).delete()
}
if (instance == null) {
instance = Room.databaseBuilder(context,CustomerDatabase::class.java,"thedatabase.db")
.allowMainThreadQueries()
.build()
}
return instance as CustomerDatabase
}
fun getDatabase(context: Context): CustomerDatabase {
if (instance == null) {
instance = Room.databaseBuilder(context,CustomerDatabase::class.java,"thedatabase.db")
.allowMainThreadQueries()
.build()
}
return instance as CustomerDatabase
}
}
}
- 请注意,除了请求重置外,还会进行检查以确保未检索到数据库实例。
这也会更有效,因为 clearAllTables 仍然会处理基础数据和随后的 VACUUM,这可能非常耗费资源。
我尝试了很多方法来重置它。
allowBackup
和fullBackupOnly
已设置为 false。
.fallbackToDestructiveMigration()
并直接删除数据库和缓存文件。
但是没用。
你可以使用这个clear all tables 这会删除作为 Database.entities().
注册到此数据库的所有表中的所有行最简单的方法是卸载应用程序,这会删除数据库文件。所以重新运行从一个全新的数据库开始。
要使用 .fallbackToDestructiveMigration()
,您必须通过增加版本号来调用迁移,但不能针对特定路径进行迁移。您可能会争辩说这不会重置数据库,因为新创建的数据库将具有更高的版本号。
使用 clearAllTables
不会完全重置数据库,因为它不会删除系统 table。最值得注意的是,sqlite_sequence,这是一个 table,它在每个 table 的基础上保存最新 rowid 的值。也就是说,如果您在 @PrimaryKey 注释中有 autogenerate = true
用于解析为 INTEGER 的列类型亲和性的 field/column,那么将对 AUTOINCREMENT 进行编码,然后将创建 sqlite_sequence table (如果不存在)并存储所述主键的最新(因此也是最高)值。因此,如果您已将 100 行(例如)插入到这样的 table 中,那么在 clearAllTables 之后,这 100 行仍将存储在 sqlite_sequnce table.
您也可以在构建数据库之前删除数据库。这是一个允许在构建时删除它的示例:-
@Database(entities = [Customer::class], version = 1)
abstract class CustomerDatabase: RoomDatabase() {
abstract fun customerDao(): CustomerDao
companion object {
private var instance: CustomerDatabase?=null
fun getDatabase(context: Context, resetDatabase: Boolean): CustomerDatabase {
if (resetDatabase && instance == null) {
(context.getDatabasePath("thedatabase.db")).delete()
}
if (instance == null) {
instance = Room.databaseBuilder(context,CustomerDatabase::class.java,"thedatabase.db")
.allowMainThreadQueries()
.build()
}
return instance as CustomerDatabase
}
fun getDatabase(context: Context): CustomerDatabase {
if (instance == null) {
instance = Room.databaseBuilder(context,CustomerDatabase::class.java,"thedatabase.db")
.allowMainThreadQueries()
.build()
}
return instance as CustomerDatabase
}
}
}
- 请注意,除了请求重置外,还会进行检查以确保未检索到数据库实例。
这也会更有效,因为 clearAllTables 仍然会处理基础数据和随后的 VACUUM,这可能非常耗费资源。