简体   繁体   中英

Room Query not working

I'm trying to use Room with LiveData in my project. In my app, I have the authors table. The data is inserted fine but when I'm trying to read something from the table it did not give me records. I also see the database with SQLite Opener software. It shows me all the data.

Below is my Authors Entity.

@Entity(tableName = "authors")
    data class AuthorModel(
    @PrimaryKey(autoGenerate = true)
    @ColumnInfo(name = "id") val id: Long = 0,
    @ColumnInfo(name = "name") var name: String
)

And this is my Author Dao interface.

@Dao
interface AuthorDao {

    @Query(value = "SELECT * FROM authors")
    fun allAuthors(): LiveData<List<AuthorModel>>

    @Query(value = "SELECT * FROM authors WHERE id = :authorId")
    fun authorWithId(authorId: Long): LiveData<AuthorModel?>

    @Insert(onConflict = OnConflictStrategy.REPLACE)
    fun insert(author: AuthorModel): Long

}

And last, this is my RoomDatabase class.

@Database(entities = [AuthorModel::class], version = 1)
abstract class BookLibraryDatabase : RoomDatabase() {

abstract fun authorDao(): AuthorDao

}

Thank you for your time.

You have to observe LiveData. livedata.value will be null when you get first time.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM