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

标签 android kotlin android-room

我正在尝试使用新的房间库,但出现此错误

Error: There is a problem with the query: [SQLITE_ERROR] SQL error or missing database (no such table: Station)

Error: Not sure how to convert a Cursor to this method's return type

Warning: Schema export directory is not provided to the annotation processor so we cannot export the schema. You can either provide room.schemaLocation annotation processor argument OR set exportSchema to false.

Error:org.gradle.api.internal.tasks.compile.CompilationFailedException: Compilation failed; see the compiler error output for details.

enter image description here

房间数据库.kt

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

MyDao.kt

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


    @get:Query("SELECT * FROM TrolleyType")
    val trolleyTypes: List<TrolleyType>
}

实体

@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? = ""
)

最佳答案

原来我只是忘记将 Station 实体添加到 RoomDatabase

添加后一切正常

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

关于android - 尝试使用 Room 库时出错。 [SQLITE_ERROR] SQL 错误或缺少数据库,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/45170399/

相关文章:

swift - 如何从 Kotlin 移动平台访问 Array 到 iOS 或 Android?

android - 类型不匹配 : inferred type is AutoMigration but KClass<*> was expected

android - 如何忽略带有房间的实体字段

android - 如何更改(删除然后添加)Android 中按钮的背景?

android - 错误 :Expected caller to ensure valid ABI: ARM64_V8A

java - 根据当前用户 uid 检查数据库条目的子值?

java - 在房间中的实体之间创建关系

android - Android Studio 中的代码分享

kotlin - 匹配6个字段中的3个的查询条件

kotlin - 如何在这里使用 lambda 表达式?