[英]How to get the row count of Room database in android?
我正在遵循擁有 Repository 和 Dao 等的做法。 我試圖通過一個函數來獲取我的數據庫存儲庫中的行數
int getNumFiles() {
List<AFile> lst = files.getValue(); // files is of type LiveData<List<AFile>> files;
if (lst == null) {
return 0;
} else {
return lst.size();
}
}
但lst
總是評估為null
。 我想這與我不允許從 UI 線程查詢數據庫有關嗎? 我應該像實現添加或刪除元素一樣實現它嗎? 換句話說,在 Dao 中有一個通過數據庫存儲庫中的 AsyncTask 調用的函數? 我對如何做這個非常簡單的事情感到困惑。
有這個答案顯示了一個人會在 Dao 中寫什么來找出行數,但它沒有解釋存儲庫應該如何調用它。
房間數據庫計數表行
@Query("SELECT COUNT(column_name) FROM tableName")
LiveData<Integer> getRowCount(); //with LiveData
@Query("SELECT COUNT(column_name) FROM tableName")
int getRowCount();
我最終這樣做了(使用新線程進行查詢)。
在道
@Query("SELECT COUNT(id) FROM table")
int getCount();
在存儲庫中
int getNumFiles() {
return afileDao.getCount();
}
我需要它的地方
final AtomicInteger fcount = new AtomicInteger();
Thread t = new Thread(new Runnable() {
@Override
public void run() {
int num = f_repo.getNumFiles();
fcount.set(num);
}
});
t.setPriority(10);
t.start();
t.join();
// use as fcount.get()
讓我們看看這是否有效。 我可能不在基地,但我一直在努力解決同樣的問題,試圖學習 Room 數據庫,最近試圖獲取我正在使用的表的行數。
(這是我的第一篇文章,所以我為它的簡短道歉,並歡迎建設性的想法讓它變得更好。)
從 Dao 開始,我用 @Query() 注釋聲明了方法。 這是我們將定義用於檢索所需信息的查詢的地方。
@Query("SELECT COUNT(*) FROM word_table")
LiveData<Integer> getCount();
其次,通過存儲庫執行此操作。 Repository 將調用我們的 Dao 類來檢索信息並傳遞查詢。
public LiveData<Integer> getCount() {
return mWordDao.getCount();
}
第三,將其帶入 ViewModel。 ViewModel 將由(在這種情況下)MainActivity 調用,然后將從 Repository 調用 getCount() 方法並返回鏈。
// count
public LiveData<Integer> getCount() { return mRepository.getCount(); }
最后,在 MainActivity 中創建 observable,就像我用 LiveData<> 包裝器封裝了值一樣。
mWordViewModel.getCount().observe(this, new Observer<Integer>() {
@Override
public void onChanged(@Nullable Integer integer) {
word_count.setText(String.valueOf(integer));
}
});
我知道這很簡單,簡短並且遺漏了很多細節,但是在多次查看房間數據庫代碼之后,這對我來說能夠顯示我引用的數據庫表中的行數. 這似乎是 Room 數據庫的工作方式。
(我用作分支以檢索行數的代碼是從 Google 為 Room Databases 第 I 部分提供的代碼庫實驗室中獲取的。)
您可以通過以下鏈接與他們聯系,然后單擊 Room Databases - Part 1: Codelabs for Android Developers
斯科特
我不需要LiveData
並且我使用了Coroutine
:
// DAO
@Query("SELECT COUNT(*) FROM some_table")
suspend fun getCount(): Int
// REPOSITORY
fun getCount(): Int = runBlocking {
val count = async {
dao.getCount()
}
count.start()
count.await()
}
// VIEWMODEL
when (val count = repository.getCount()) {
// do stuff with count
}
我認為在后台線程中做微型事情的更好方法是創建一個Handler
& HandlerThread
並使用它們來執行一個線性任務。
//The handlers to perform tasks on the background threads
override lateinit var mHandler: Handler
override lateinit var mHandlerThread: HandlerThread
override fun start() {
//Instantiate the handlerThread
mHandlerThread = HandlerThread(MainPresenter::class.java.simpleName)
//A call to the start method has to be executed manually
mHandlerThread.start()
mHandler = Handler(mHandlerThread.looper)
}
無論您想在后台線程中調用什么,只需:
mHandler.post { getTableCountInBg() }
我正在輸入@Sameer Donga鏈接到的內容,但請改為參考。 像上面那樣稱呼它。
PS 忽略覆蓋注釋。 他們在那里是因為我對演示者強制執行它。
@Query("SELECT COUNT(DISTINCT column_name) FROM table)
LiveData<Integer> getTotalNumberOfRows();
將 DISTINCT 作為參數添加到 COUNT 函數。
@Query("SELECT COUNT(column_name) FROM table)
LiveData getTotalNumberOfColumns();
或者如果您不希望列中的值多次出現,請執行此操作
@Query("SELECT COUNT(DISTINCT column_name) FROM table)
LiveData getTotalNumberOfColumns();
聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.