我的应用程序发布后,发生了以下崩溃。
Fatal Exception: android.database.sqlite.SQLiteDiskIOException: disk I/O error (code 1802 SQLITE_IOERR_FSTAT[1802]): , while compiling: PRAGMA cache_size
at android.database.sqlite.SQLiteConnection.nativePrepareStatement(SQLiteConnection.java)
at android.database.sqlite.SQLiteConnection.acquirePreparedStatement(SQLiteConnection.java:1540)
at android.database.sqlite.SQLiteConnection.executeForLong(SQLiteConnection.java:1075)
at android.database.sqlite.SQLiteConnection.setCacheSizeFromConfiguration(SQLiteConnection.java:536)
at android.database.sqlite.SQLiteConnection.open(SQLiteConnection.java:416)
at android.database.sqlite.SQLiteConnection.open(SQLiteConnection.java:228)
at android.database.sqlite.SQLiteConnectionPool.openConnectionLocked(SQLiteConnectionPool.java:758)
at android.database.sqlite.SQLiteConnectionPool.tryAcquireNonPrimaryConnectionLocked(SQLiteConnectionPool.java:1355)
at android.database.sqlite.SQLiteConnectionPool.waitForConnection(SQLiteConnectionPool.java:983)
at android.database.sqlite.SQLiteConnectionPool.acquireConnection(SQLiteConnectionPool.java:619)
at android.database.sqlite.SQLiteSession.acquireConnection(SQLiteSession.java:916)
at android.database.sqlite.SQLiteSession.prepare(SQLiteSession.java:588)
at android.database.sqlite.SQLiteProgram.<init>(SQLiteProgram.java:63)
at android.database.sqlite.SQLiteQuery.<init>(SQLiteQuery.java:37)
at android.database.sqlite.SQLiteDirectCursorDriver.query(SQLiteDirectCursorDriver.java:46)
at android.database.sqlite.SQLiteDatabase.rawQueryWithFactory(SQLiteDatabase.java:2278)
at android.database.sqlite.SQLiteDatabase.rawQueryWithFactory(SQLiteDatabase.java:2253)
at androidx.sqlite.db.framework.FrameworkSQLiteDatabase.query(FrameworkSQLiteDatabase.kt:156)
at androidx.room.RoomDatabase.query(RoomDatabase.kt:490)
at androidx.room.util.DBUtil.query(DBUtil.kt:75)
at myapppackage.data.datasource.db.language.MultiLanguageStationNameDao_Impl$1.call(MultiLanguageStationNameDao_Impl.java:37)
at myapppackage.data.datasource.db.language.MultiLanguageStationNameDao_Impl$1.call(MultiLanguageStationNameDao_Impl.java:34)
at androidx.room.CoroutinesRoom$Companion$createFlow$1$1$1.invokeSuspend(CoroutinesRoom.kt:128)
at kotlin.coroutines.jvm.internal.BaseContinuationImpl.resumeWith(ContinuationImpl.kt:33)
at kotlinx.coroutines.DispatchedTask.run(DispatchedTask.kt:106)
at java.util.concurrent.ThreadPoolExecutor.runWorker(ThreadPoolExecutor.java:1145)
at java.util.concurrent.ThreadPoolExecutor$Worker.run(ThreadPoolExecutor.java:644)
at java.lang.Thread.run(Thread.java:1012)
上一版本首次使用了Room库,下一版本已更新数据库。 这是房间数据库构建器代码。
private fun buildDatabase(context: Context): AppDatabase {
return Room.databaseBuilder(
context,
AppDatabase::class.java, "new_database.db"
).createFromAsset("db/my_app_db.sqlite")
.fallbackToDestructiveMigration()
.build()
}
sqlite 文件是从 asset 中导入的,并且新的数据已添加到该 sqlite 文件中。 但是新添加的数据在app中并没有查询到,所以查了一下发现需要增加版本,于是就在版本上加了1,如下图。 (之前1)
@Database(
entities = [
MultiLanguageStationNameEntity::class,
],
version = 2
)
存储库
override fun getStationNames(): Flow<List<MultiLanguageStationNameRes>> {
return multiLanguageStationNameDao.getStationNames()
.map { entities ->
entities.map {
entity -> entity.asDomainModel()
}
}
}
道
@Dao
interface MultiLanguageStationNameDao {
@Query("SELECT * FROM MultiLanguageStationName")
fun getStationNames(): Flow<List<MultiLanguageStationNameEntity>>
}
我想知道是什么原因导致了崩溃。
这是因为应用程序是在数据库打开时更新的吗?
据我所知,fallbackToDestructiveMigration() 将删除并重新创建数据库。
这会有影响吗?
如果应用程序在迁移过程发生时仍然访问或打开数据库,则可能会发生这种情况。