我已经将我的应用程序转换为使用SQLite DB的Android。使用我的实现,在不同的设备上有一些崩溃。
Fatal Exception: android.database.sqlite.SQLiteReadOnlyDatabaseException: attempt to write a readonly database (code 1032 SQLITE_READONLY_DBMOVED)
at android.database.sqlite.SQLiteConnection.nativeExecute(SQLiteConnection.java)
at android.database.sqlite.SQLiteConnection.execute(SQLiteConnection.java:707)
at android.database.sqlite.SQLiteConnection.setLocaleFromConfiguration(SQLiteConnection.java:473)
at android.database.sqlite.SQLiteConnection.open(SQLiteConnection.java:261)
at android.database.sqlite.SQLiteConnection.open(SQLiteConnection.java:205)
at android.database.sqlite.SQLiteConnectionPool.openConnectionLocked(SQLiteConnectionPool.java:505)
at android.database.sqlite.SQLiteConnectionPool.open(SQLiteConnectionPool.java:206)
at android.database.sqlite.SQLiteConnectionPool.open(SQLiteConnectionPool.java:198)
at android.database.sqlite.SQLiteDatabase.openInner(SQLiteDatabase.java:918)
at android.database.sqlite.SQLiteDatabase.open(SQLiteDatabase.java:898)
at android.database.sqlite.SQLiteDatabase.openDatabase(SQLiteDatabase.java:762)
at android.database.sqlite.SQLiteDatabase.openDatabase(SQLiteDatabase.java:751)
at android.database.sqlite.SQLiteOpenHelper.getDatabaseLocked(SQLiteOpenHelper.java:373)
at android.database.sqlite.SQLiteOpenHelper.getWritableDatabase(SQLiteOpenHelper.java:316)
at androidx.sqlite.db.framework.FrameworkSQLiteOpenHelper$OpenHelper.getWritableSupportDatabase(FrameworkSQLiteOpenHelper.java:145)
at androidx.sqlite.db.framework.FrameworkSQLiteOpenHelper.getWritableDatabase(FrameworkSQLiteOpenHelper.java:106)
at androidx.room.SQLiteCopyOpenHelper.getWritableDatabase(SQLiteCopyOpenHelper.java:97)
at androidx.room.RoomDatabase.internalBeginTransaction(RoomDatabase.java:482)
at androidx.room.RoomDatabase.beginTransaction(RoomDatabase.java:471)
at com.luzeon.MyApp.sqlite.ViewLogDao_Impl$5.call(ViewLogDao_Impl.java:94)
at com.luzeon.MyApp.sqlite.ViewLogDao_Impl$5.call(ViewLogDao_Impl.java:91)
at androidx.room.CoroutinesRoom$Companion$execute$2.invokeSuspend(CoroutinesRoom.kt:61)
at kotlin.coroutines.jvm.internal.BaseContinuationImpl.resumeWith(ContinuationImpl.kt:33)
at kotlinx.coroutines.DispatchedTask.run(DispatchedTask.kt:106)
at androidx.room.TransactionExecutor$1.run(TransactionExecutor.java:47)
at java.util.concurrent.ThreadPoolExecutor.runWorker(ThreadPoolExecutor.java:1167)
at java.util.concurrent.ThreadPoolExecutor$Worker.run(ThreadPoolExecutor.java:641)
at java.lang.Thread.run(Thread.java:923)我已经创建了应用程序室DB
@Database(entities = [ViewLogModel::class], version = 4)
abstract class MyAppDatabase : RoomDatabase() {
abstract fun viewLogDao(): ViewLogDao
companion object {
// For Singleton Instance
@Volatile
private var INSTANCE: MyAppDatabase? = null
fun getAppDataBase(context: Context): MyAppDatabase {
return INSTANCE ?: synchronized(this) {
INSTANCE ?: Room.databaseBuilder(context.applicationContext, MyAppDatabase::class.java, "MyAppDatabase")
.createFromAsset(“myapp.db")
.setJournalMode(RoomDatabase.JournalMode.TRUNCATE) // disable WAL
.fallbackToDestructiveMigration()
.build()
}
}
fun destroyDataBase(){
INSTANCE = null
}
}}
并有一个DB Helper类
class MyAppDatabaseHelper(private val context: Context, private val coroutineScope: CoroutineScope) {
fun updateViewLog(viewLogModel: ViewLogModel) {
try {
// get the database
val db = MyAppDatabase.getAppDataBase(context)
coroutineScope.launch {
// store in db
db.viewLogDao().insertOrUpdateViewLog(viewLogModel)
}
} catch (e: Exception) {}
}
suspend fun getViewLog(memberId: Int): JSONArray {
try {
val jsonArray = JSONArray()
// get the database
val db = MyAppDatabase.getAppDataBase(context)
val viewLog = db.viewLogDao().getViewLog(memberId)
for (view in viewLog) {
// create the object
val jsonObject = JSONObject()
try {
jsonObject.put("mid", view.mid)
} catch (e: JSONException) {
}
try {
jsonObject.put("uts", view.uts)
} catch (e: JSONException) {
}
jsonArray.put(jsonObject)
}
// clear log (current user records or records older than 24hrs)
db.viewLogDao().deleteViewLog(memberId, Utilities.getUtsYesterday().toFloat())
// return the array
return jsonArray
} catch (e: Exception) {
return JSONArray()
}
}}
用ViewLogDao
@Dao
interface ViewLogDao {
@Insert(onConflict = OnConflictStrategy.REPLACE)
suspend fun insertOrUpdateViewLog(viewLog: ViewLogModel)
@Update
suspend fun updateViewLog(viewLog: ViewLogModel)
@Delete
suspend fun deleteAllViewLog(viewLog: ViewLogModel)
@Query("DELETE FROM viewlog WHERE VID= :vid OR UTS < :uts")
suspend fun deleteViewLog(vid: Int, uts: Float)
@Query("SELECT * FROM viewLog")
suspend fun getAll(): List<ViewLogModel>
@Query("SELECT * FROM viewLog WHERE vid = :vid")
suspend fun getViewLog(vid: Int): List<ViewLogModel>}
和ViewLogModel
@Entity(tableName = "viewLog")
data class ViewLogModel(
@ColumnInfo(name = "VID") val vid: Int,
@ColumnInfo(name = "UTS") val uts: Float,
@ColumnInfo(name = "MID") @PrimaryKey val mid: Int)当数据库被只读时,我还没有找到如何在罕见的情况下捕获SQLiteReadOnlyDatabaseException。还是有办法确保房间数据库是读/写的?
发布于 2021-11-04 17:16:00
当数据库被只读时,我还没有找到如何在罕见的情况下捕获SQLiteReadOnlyDatabaseException。还是有办法确保房间数据库是读/写的?
信息code 1032 SQLITE_READONLY_DBMOVED :-
SQLITE_READONLY_DBMOVED错误代码是SQLITE_READONLY的扩展错误代码。SQLITE_READONLY_DBMOVED错误代码表示无法修改数据库,因为数据库文件自打开以来已被移动,因此,如果进程因回滚日志不能正确命名而导致数据库崩溃,则任何修改数据库的尝试都可能导致数据库损坏。
如果要相信该消息,则数据库已被移动/重命名。从消息中可以看出,(两个正在处理的数据库之一)在打开时正在被重命名。
在日志中,许多条目是相似的,因此看起来有两个数据库正在被管理,即这是从资产创建数据库的阶段。
这很可能是createFromAsset处理中的一个问题,据我所知,这并不一定是可靠的。目前有关于prePackagedDatabaseCallback的问题。
因此,通过使用createFromAsset,您只能提出一个问题。
我建议你先绕过这个问题,在把控制权交给会议室之前,自己先复制一下资产。
另一种选择,可能是看看是否只使用WAL模式,解决了这个问题。由于您正在禁用WAL模式,那么我猜您不希望这样做(因此建议将其作为最后一种模式)。
https://stackoverflow.com/questions/69840310
复制相似问题