如何在android中获取Room数据库的行数?

如何在android中获取Room数据库的行数?,android,android-room,Android,Android Room,我遵循的做法是拥有一个存储库和一个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

我遵循的做法是拥有一个存储库和一个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();
    }
}
int getNumFiles(){

列出答案,其中显示了要在Dao中编写什么来找出行数,但它没有解释存储库应该如何调用它。

我最后是这样做的(使用一个新线程进行查询)

道中

@Query("SELECT COUNT(id) FROM table WHERE is_checked = 1")
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()

我认为在后台线程中进行微型操作的更好方法是创建
处理程序
&
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() }
我正在输入链接到的内容,但请参考它。如上所述


请忽略覆盖批注。它们在那里是因为我在演示者上强制执行了它。

房间数据库计数表行

@Query("SELECT COUNT(column_name) FROM tableName")
LiveData<Integer> getRowCount();
@Query(“从tableName中选择COUNT(列名称))
LiveData getRowCount();

让我们看看这是否有效。我可能有点离谱,但我在尝试学习Room数据库时遇到了同样的问题,最近尝试获取我正在使用的表的行数

(这是我的第一篇帖子,因此我为它的短处道歉,并欢迎有建设性的想法来改进它。)

从Dao开始,我用@Query()注释声明了该方法。在这里,我们将定义用于检索所需信息的查询

@Query("SELECT COUNT(*) FROM word_table")
LiveData<Integer> getCount();
@Query(“从word_表中选择计数(*))
LiveData getCount();
第二,在存储库中执行此操作。存储库将调用我们的Dao类来检索信息并基本上传递查询

public LiveData<Integer> getCount() {
    return mWordDao.getCount();
}
public LiveData getCount(){
返回mWordDao.getCount();
}
第三,将其引入ViewModel。ViewModel将由(在本例中)MainActivity调用,然后从存储库中调用getCount()方法并返回链

// count
public LiveData<Integer> getCount() { return mRepository.getCount(); }
//计数
public LiveData getCount(){return mRepository.getCount();}
最后,在MainActivity中创建observable,因为我用LiveData包装器封装了值

mWordViewModel.getCount().observe(这是一个新的观察者(){
@凌驾
公共void onChanged(@Nullable Integer){
word_count.setText(String.valueOf(integer));
}
});
我知道这过于简单、简短,并且遗漏了很多细节,但在对房间数据库代码进行了大量检查之后,这对我来说非常有用,因为我能够显示我所引用的数据库表中的行数。这似乎是房间数据库的工作方式

(我用来作为分支检索行数的基础的代码是从Google为Room Databases提供的codebase labs第一部分中获取的。)

您可以通过以下链接联系他们,并单击房间数据库-第1部分:


Scott

我不需要
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
}
@Query(“从表中选择计数(不同的列名称))
LiveData GetTotalNumber of Columns();
将DISTINCT作为参数添加到COUNT函数。

@Query(“从表中选择COUNT(列名称))

LiveData GetTotalNumber of Columns()

或者,如果不希望某个值在列中多次出现,请执行此操作

@查询(“从表中选择计数(不同的列名称))


LiveData GetTotalNumber of Columns()

尝试此文件室数据库表行数如何使用文件室从一个查询中获取多个计数?如果表为空,查询是否返回null?提供的答案被标记为低质量帖子以供审阅。以下是一些指导原则。此提供的答案可以从解释中受益。仅代码的答案不被视为“好”“答案,来自。
// 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
}
@Query("SELECT  COUNT(DISTINCT column_name) FROM table)
LiveData<Integer> getTotalNumberOfColumns();