【问题标题】:error: There is a problem with the query: [SQLITE_ERROR] SQL error or missing database (no such column: isFinished)错误:查询有问题:[SQLITE_ERROR] SQL 错误或缺少数据库(没有这样的列:isFinished)
【发布时间】:2020-10-13 09:12:15
【问题描述】:

使用 Room + rxJava,我想在我的数据库表“story”中添加额外的列,但不在实际数据库中,所以我使用“@Transient”,它的工作方式类似于“@Ignore”。

我还有 2 个数据模型和 2 个访问数据库的“Dao”。 添加后“词汇表”数据模型工作正常:

@Transient
var status: Int? = 0

但是当我想像这样向我的第二个数据模型“故事”添加另一列时:

@Transient
var isFinished: Int? = 0

它给了我这个错误:

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

这是数据模型:

import androidx.room.Entity
import androidx.room.Ignore
import androidx.room.PrimaryKey


@Entity(tableName = "vocabulary")
data class  Vocabulary constructor(

@PrimaryKey
val id: Int?,
val range: String?,
val note: String?,
val word: String?,
val coding: String?,
val pronunciation: String?,
val examples: String?,
val lesson: Int?,
val lesson_order: Int?,
val definition: String?,
val persian: String?,
var favorited: Int?,
val viewed: Int?,
val is_read: Int?,
val pexa: String?,
val pexb: String?,
val pexc: String?
){
    @Transient
    var status: Int? = 0
}

第二个数据模型:

import androidx.room.Entity
import androidx.room.Ignore
import androidx.room.PrimaryKey


@Entity(tableName = "story")
data class Story(

    @PrimaryKey
    val id: Int?,
    val title: String?,
    val text: String?,
    val per_title: String?,
    val per_text: String?,
    val range: String?

){
    @Transient
    var isFinished: Int? = 0
}

在道中我是这样使用它的:

@Dao
interface IStoryDao {

    @Query("UPDATE story SET isFinished = 1 ")
    fun setFinished(): Completable
}

【问题讨论】:

    标签: android database kotlin android-room rx-java2


    【解决方案1】:

    但是你期待什么呢?

    @Transient like @Ignore 允许向实体类添加字段无需向 Sqlite 数据库添加相应的列。这意味着您不能在查询中使用此类字段,并且错误消息准确地说明了这一点:

    没有这样的列:isFinished

    也许你在下面有一些误解?

    我想在我的数据库表“故事”中添加额外的列,但不在实际数据库中

    使用@Transient,您只需将字段添加到 Kotlin 数据类,并且该字段不会保留在数据库中。我猜你对status 字段没有问题,只是因为你不使用查询。

    【讨论】:

    • 感谢您的回复!现在清楚多了!现在我明白我是如何错误地使用它了!谢谢人
    猜你喜欢
    • 2019-03-04
    • 2023-01-24
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2014-06-03
    • 1970-01-01
    • 1970-01-01
    • 2021-07-19
    相关资源
    最近更新 更多