首页 > 解决方案 > 如何从房间数据库中获取字符串

问题描述

我需要从我的房间数据库中获取一个字符串并将其设置为文本视图中的文本我在我的 dao 中使用此代码

    @Query("SELECT question, question_heat, question_gender FROM questions WHERE question_heat = :heat AND question_gender = :gender" +
            " ORDER BY RANDOM() LIMIT 1")
    String getQuestion(int heat, int gender);

我只想从我的问题数据库中获取一个随机问题。我收到此错误:

error: Not sure how to convert a Cursor to this method's return type String getQuestion(int heat, int gender);

在构建输出中说错误在查询中

我在房间里真的很新,我使用 sqlopenhelper 有一段时间了,我真的不知道在这里做什么。

我在谷歌找到了一些代码,但它们是用于数据列表的,我只想得到一个字符串。

标签: javaandroidandroid-room

解决方案


您必须只选择question列。试试这个方法:

@Query("SELECT question FROM questions WHERE question_heat = :heat AND question_gender = :gender ORDER BY RANDOM() LIMIT 1")
String getQuestion(int heat, int gender);

推荐阅读