尝试使用Room库时出错.[SQLITE_ERROR] SQL错误或缺少数据库

hum*_*zed 3 android kotlin android-room

我正在尝试使用新的房间库,但我收到了这个错误

错误:查询有问题:[SQLITE_ERROR] SQL错误或缺少数据库(没有这样的表:Station)

错误:不确定如何将Cursor转换为此方法的返回类型

警告:未向注释处理器提供架构导出目录,因此我们无法导出架构.您可以提供 room.schemaLocation注释处理器参数或将exportSchema设置为false.

错误:org.gradle.api.internal.tasks.compile.CompilationFailedException:编译失败; 请参阅编译器错误输出以获取详细信

在此输入图像描述

RoomDatabase.kt

@Database(entities = TrolleyType::class), version = 2)
abstract class AppDatabase : RoomDatabase() {
    abstract fun stationDao(): StationDao
}
Run Code Online (Sandbox Code Playgroud)

MyDao.kt

@Dao
interface MyDao {
    @get:Query("SELECT * FROM Station")
    val stations: List<Station>


    @get:Query("SELECT * FROM TrolleyType")
    val trolleyTypes: List<TrolleyType>
}
Run Code Online (Sandbox Code Playgroud)

的entites

@Entity
data class Station(
        @PrimaryKey @ColumnInfo(name = "_id") var id: Int = 0,
        @ColumnInfo(name = "StationName") var stationName: String? = "",
        @ColumnInfo(name = "StationArabic") var stationArabic: String? = ""
)

@Entity
data class TrolleyType(
        @PrimaryKey @ColumnInfo(name = "_id") var id: Int = 0,
        @ColumnInfo(name = "Type") var stationName: String? = "",
        @ColumnInfo(name = "TypeArabic") var stationArabic: String? = ""
)
Run Code Online (Sandbox Code Playgroud)

hum*_*zed 19

事实证明,我忘了将Station实体添加到RoomDatabase

添加后,每件事都按预期工作

@Database(entities = TrolleyType::class, Station::class), version = 2)
abstract class AppDatabase : RoomDatabase() {
    abstract fun stationDao(): StationDao
}
Run Code Online (Sandbox Code Playgroud)