【发布时间】:2022-03-07 23:11:29
【问题描述】:
我正在使用 espresso 清除我的应用程序中的数据库 我像这样设置活动
@Rule
@JvmField
val activity = ActivityTestRule<PhotoPrinterActivity>(PhotoPrinterActivity::class.java,false,false)
这是我之前的功能
@Before
open fun setup() {
clearDatabase()
activity.launchActivity(null)
// Waiting for start app success fully
}
这是我清晰的数据库代码
fun clearDatabase() {
val databaseList = InstrumentationRegistry.getInstrumentation().targetContext.databaseList()
for (database in databaseList) {
// when transaction rollback files exists they are always locked so we can't delete them
if (database.contains(".db-journal")) {
InstrumentationRegistry.getTargetContext().deleteDatabase(database)
continue
}
// when using transaction write ahead logging then this db files are listed but often they don't exist
if (database.contains(".db-wal") || database.contains(".db-shm")) {
InstrumentationRegistry.getTargetContext().deleteDatabase(database)
continue
}
Log.v("EspressoMacchiato", "deleting " + database)
var databasePath = InstrumentationRegistry.getInstrumentation().targetContext.getDatabasePath(database)
if (databasePath.exists()) {
InstrumentationRegistry.getInstrumentation().targetContext.deleteDatabase(database)
}
}
}
问题是当清除数据库成功并执行向数据库中添加一些数据时,
android.database.sqlite.SQLiteReadOnlyDatabaseException: attempt to write a readonly database (code 1032)
at android.database.sqlite.SQLiteConnection.nativeExecuteForLastInsertedRowId(Native Method)
at android.database.sqlite.SQLiteConnection.executeForLastInsertedRowId(SQLiteConnection.java:786)
at android.database.sqlite.SQLiteSession.executeForLastInsertedRowId(SQLiteSession.java:788)
任何人请帮助我!非常感谢!
【问题讨论】:
-
删除操作可能会锁定数据库文件,这就是您会看到此错误的原因。尝试在您的活动启动时发布延迟以测试是否是这种情况。
-
你能把你的数据库代码贴出来
标签: android kotlin ui-automation android-espresso