簡體   English   中英

Room Database查詢不返回數據,即使數據庫中有數據

[英]Room Database query not returning data, even though there is data in the database

我在DAO類中使用此方法來訪問數據庫中的數據:

@Query("select * from location_search_results WHERE searchQuery = :query")
fun searchForLocation(query: String): LiveData<LocationSearchResponse>

我這樣稱呼它:

locationResponse = locationResponseDao.searchForLocation(query)
Log.d("db", locationResponse.value.toString())

然后,我觀察到這樣的值:

    fun bindUI() = launch {
        val locationResults = locationViewModel.locationResponse
        val owner = viewLifecycleOwner

        locationResults.observe(owner, Observer {
            if (it == null) return@Observer

            initRecyclerView(it.features.toLocationSearchResultListItem())
        })
    }

我的問題是,即使數據庫中有與查詢匹配的數據,只要執行此操作,觀察者總是返回null。 我還嘗試過使用select * from location_search_results進行嘗試,以防searchQuery參數出現問題,但是也返回null。

這是數據庫的樣子:

數據庫

我是Room和SQLite的新手,所以非常感謝您的幫助。 謝謝。

由於您要從room查詢返回livedata,因此應注意獲取該值。 類似於:locationResponseDao.searchForLocation(query).observeForever {響應-> locationResponse.value =響應

}

暫無
暫無

聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM