user12933357
user12933357

Reputation:

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.

Upvotes: 2

Views: 1087

Answers (2)

Ade Dyas
Ade Dyas

Reputation: 53

You select 3 columns and expect to get a string?

I think you 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 manually in repository with

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

Upvotes: 0

Garnik
Garnik

Reputation: 145

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);

Upvotes: 1

Related Questions