简体   繁体   中英

How to get just a string from room database

I need to get a string from my room database and set it as text in a textview i'm using this code in my 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);

i just want to get a random question from my question database.I get this error:

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

in the build output says the error is in the query

i'm really new in room i was using sqlopenhelper for a while and i don't really know what to do here.

i found some codes in google but they were for lists of data and i want to get just a string.

You must select only question column. Try this way:

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

you select 3 coloumn and expect to get string? i think u must do this :

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

and then convert it manualy in repository with

String result = question.getQuestion + question.getQuestionHeat + question.getQuestionGender ;

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