Tag Archives: Error reporting Guide

How to Solve Jetpack room use error

 

When building the room, there are several small problems. Record them. Basically, it is a configuration problem:

There is a problem with the query: [SQLITE_ERROR] SQL error or missing database (no such table: simple_student)。
an error is reported during compilation and the table cannot be found
you need to set the corresponding indication in the declared entity.

//Wrong
@Entity
data class StudentEntity
//Right
@Entity(tableName = "student_table")
data class StudentEntity

Not sure how to convert a Cursor to this method’s return type (kotlinx.coroutines.flow.Flow<?extends java.util.List<com.zxf.jetpackrelated.room.liveDataOrFlow.StudentEntity>>).

When the Dao layer declares the return value as livedata, an error is reported. After a long investigation, it was found that the corresponding function was declared as a suspended function
correct use should remove the corresponding suspend modifier.

//Wrong
@Query("select * from $STUDENT_TABLE_NAME")
suspend fun obtainStudentAll(): LiveData<List<StudentEntity>>
//Right
@Query("select * from $STUDENT_TABLE_NAME")
fun obtainStudentAll(): LiveData<List<StudentEntity>>

Not sure how to convert a Cursor to this method’s return type (androidx.lifecycle.LiveData<java.util.List<com.zxf.jetpackrelated.room.liveDataOrFlow.StudentEntity>>)
Similarly, it cannot be converted to flow, and the corresponding suspend modifier is removed.

//Wrong
@Query("select * from $STUDENT_TABLE_NAME")
suspend fun obtainStudentAll(): Flow<List<StudentEntity>>
//Right
@Query("select * from $STUDENT_TABLE_NAME")
fun obtainStudentAll(): Flow<List<StudentEntity>>

If you do not need to declare the return type as livedata or flow, you can directly declare suspend.