Room 使用中遇到的错误

错误一

执行如下
private var migration2_3 = object : Migration(2, 3) {
            override fun migrate(database: SupportSQLiteDatabase) {
                database.execSQL("ALTER TABLE 'tasks' ADD COLUMN 'last_update' INTEGER ")
            }
        }
发生异常:
.IllegalStateException: Migration didn't properly handle tasks(googleroom.android.com.google_room.data.Task).
     Expected:
原因: Migration 时 添加列 新增字段未设置默认值
修改如下
database.execSQL("ALTER TABLE 'tasks' ADD COLUMN 'last_update' INTEGER NOT NULL DEFAULT 0")

错误二

写入数据时发生如下异常:
/googleroom.android.com.google_room E/ROOM: Cannot run invalidation tracker. Is the db closed?
    java.lang.IllegalStateException: The database '/data/user/0/googleroom.android.com.google_room/databases/Android-P.db' is not open.
        at android.database.sqlite.SQLiteDatabase.throwIfNotOpenLocked(SQLiteDatabase.java:2529)
        at android.database.sqlite.SQLiteDatabase.createSession(SQLiteDatabase.java:387)
        at android.database.sqlite.SQLiteDatabase$1.initialValue(SQLiteDatabase.java:99)
        at android.database.sqlite.SQLiteDatabase$1.initialValue(SQLiteDatabase.java:97)
        at java.lang.ThreadLocal.setInitialValue(ThreadLocal.java:180)
        at java.lang.ThreadLocal.get(ThreadLocal.java:170)
        at android.database.sqlite.SQLiteDatabase.getThreadSession(SQLiteDatabase.java:381)
        at android.database.sqlite.SQLiteProgram.getSession(SQLiteProgram.java:106)
        at android.database.sqlite.SQLiteStatement.executeUpdateDelete(SQLiteStatement.java:64)
        at android.arch.persistence.db.framework.FrameworkSQLiteStatement.executeUpdateDelete(FrameworkSQLiteStatement.java:45)
        at android.arch.persistence.room.InvalidationTracker$1.run(InvalidationTracker.java:321)
        at java.util.concurrent.ThreadPoolExecutor.runWorker(ThreadPoolExecutor.java:1162)
        at java.util.concurrent.ThreadPoolExecutor$Worker.run(ThreadPoolExecutor.java:636)
        at java.lang.Thread.run(Thread.java:784)

错误三

数据库升级,添加新的表

 /**
         * 新增数据库表
         */
        private var migration3_4 = object : Migration(3, 4) {
            override fun migrate(database: SupportSQLiteDatabase) {
                Log.d("---->", "<-----------Migration 3---->4")
                database.execSQL("CREATE TABLE user (name TEXT, age INTEGER, sex TEXT, PRIMARY KEY(id))")
            }

        }
异常
googleroom.android.com.google_room E/AndroidRuntime: FATAL EXCEPTION: Thread-5
    Process: googleroom.android.com.google_room, PID: 6612
    android.database.sqlite.SQLiteException: no such column: id (Sqlite code 1): , while compiling: CREATE TABLE user (name TEXT, age INTEGER, sex TEXT, PRIMARY KEY(id)), (OS error - 2:No such file or directory)
        at android.database.sqlite.SQLiteConnection.nativePrepareStatement(Native Method)
错误原因 SQL 语句错误
 database.execSQL("CREATE TABLE user (id INTEGER ,name TEXT, age INTEGER, sex TEXT, PRIMARY KEY(id))")

开启输出模式

可以在gradle中设置开启输出模式,便于我们调试,查看数据库表情况,以及做数据库迁移

android {
    ...
    defaultConfig {
        ...
        javaCompileOptions {
            annotationProcessorOptions {
                arguments = ["room.schemaLocation":
                             "$projectDir/schemas".toString()]
            }
        }
    }
}
Room 使用中遇到的错误_第1张图片
image.png
apply plugin: 'kotlin-kapt'

 /*------Room-----*/
implementation "android.arch.persistence.room:runtime:1.1.0"
kapt "android.arch.persistence.room:compiler:1.1.0"

你可能感兴趣的:(Room 使用中遇到的错误)