【Bug修复】Room数据库 The columns returned by the query does not have the fields......

BUG信息

使用Room数据库进行数据存储和读取的时候,出现了这个BUG

The columns returned by the query does not have the fields [chartId] in com.xxx.xxx.bean.
ChartRoom even though they are annotated as non-null or primitive. 
Columns returned by the query: [type_name,recently_time,type_target_sign]
public abstract java.util.List<com.xxx.xxx.bean.ChartRoom> getAll();

代码

Entry
@Fts4
@Entity(tableName = "user_healthy_data")
data class ChartRoom(
    @PrimaryKey(autoGenerate = true) @ColumnInfo(name = "rowid")
    var chartId: Long,

    @ColumnInfo(name = "type_name")
    var chartName: String?,

    @ColumnInfo(name = "recently_time")
    var chartTime: String?,

    @ColumnInfo(name = "type_target_sign")
    var chartSign: String?
)
DAO
@Dao
interface ChartRoomDAO {
    @Insert(onConflict = OnConflictStrategy.REPLACE)
    fun insertAll(vararg chartRooms: ChartRoom)

    @Update
    fun updateData(vararg chartRooms: ChartRoom)

    @Delete
    fun delete(vararg chartRooms: ChartRoom)

    @Query("SELECT * FROM user_healthy_data")
    fun getAll(): MutableList<ChartRoom>

    @Query("SELECT * FROM user_healthy_data WHERE rowid = (:chartId)")
    fun findChartRoomById(chartId: Long): ChartRoom
}
Database
@Database(entities = [ChartRoom::class], version = 1, exportSchema = false)
abstract class ChartRoomDatabase : RoomDatabase() {
    abstract fun chartRoomDao(): ChartRoomDAO?

    companion object {
        private const val DB_NAME = "user_healthy"

        @Volatile
        private var instance: ChartRoomDatabase? = null
        fun getInstance(context: Context) =
            instance ?: synchronized(this) {
                instance ?: Room.databaseBuilder(context, ChartRoomDatabase::class.java, DB_NAME)
                    .build()
            }
    }
}

代码调用

val userDB = ChartRoomDatabase.getInstance(requireActivity()).chartRoomDao()
        Thread {
            userDB?.apply {
                insertAll(
                    ChartRoom(
                        chartId = 1,
                        chartName = "心率数据",
                        chartTime = "2023-01-06",
                        chartSign = "85"
                    ),
                    ChartRoom(
                        chartId = 2,
                        chartName = "体温数据",
                        chartTime = "2023-02-28",
                        chartSign = ""
                    )
                )
                getAll().forEach {
                    Log.e(TAG, "数据库数据:${it.chartName},${it.chartTime},${it.chartSign}")
                }
            }
        }.start()
报错原因

当我们添加数据后,使用【getAll】方法获取数据库的数据,返回【ChartRoom】类型的数据,这里面有四个类型的数据,但是在获取数据时获取不到【chartId】的数据,只能返回三个数据,但是【getAll】使用【ChartRoom】接收,导致类型无法匹配,产生报错。

解决方式
创建新Bean类
data class ChartRoomBean(

    @ColumnInfo(name = "type_name")
    var chartName: String?,

    @ColumnInfo(name = "recently_time")
    var chartTime: String?,

    @ColumnInfo(name = "type_target_sign")
    var chartSign: String?
)

然后使用新的Bean类接收数据,修改【DAO】

@Dao
interface ChartRoomDAO {
    @Insert(onConflict = OnConflictStrategy.REPLACE)
    fun insertAll(vararg chartRooms: ChartRoom)

    @Update
    fun updateData(vararg chartRooms: ChartRoom)

    @Delete
    fun delete(vararg chartRooms: ChartRoom)

    @Query("SELECT * FROM user_healthy_data")
    fun getAll(): MutableList<ChartRoomBean>//将ChartRoom改为ChartRoomBean

    @Query("SELECT * FROM user_healthy_data WHERE rowid = (:chartId)")
    fun findChartRoomById(chartId: Long): ChartRoomBean//将ChartRoom改为ChartRoomBean
}

此时Bug修复。

提醒

此Bug只有在使用【@Fts】时才会出现

你可能感兴趣的:(Android错误记录,数据库,bug,java)