0

Here is my Table:

@Entity(tableName = "user_data")
data class UserData(
    @PrimaryKey(autoGenerate = true) val id: Int,
    @ColumnInfo(name = "matched_users") var matchedUsers: ArrayList<String>
)

I want a Boolean query to see if matchedUsers contains the passed-in string:

@Dao
interface UserDataDao {
    @Query("SELECT * FROM user_data WHERE :matchId IN matched_users")
    fun matchedBefore(matchId: String): Boolean
}

Obviously my query is wrong, but how can I achieve this?

PS: If ArrayList is not possible, can I use List<> or Set<>?

Zorgan
  • 8,227
  • 23
  • 106
  • 207
  • How are you storing array list in room , are you using typeconverter ? – Manohar Aug 24 '19 at 07:17
  • I don't understand your entity class , matchedUsers is autogenerated ? then whats the use of your entity – Manohar Aug 24 '19 at 07:20
  • Sorry I'm still new to Room. This is all I have at the moment. I just want to store an `ArrayList` of strings, and then be able to query to see if the arraylist contains a string. @ManoharReddy – Zorgan Aug 24 '19 at 07:24

1 Answers1

0

Instead of making matchedUsers is autogenerated , take another property in UserData class , id an Int which will be @PrimaryKey and auto generated .

You can not directly store an arraylist in room , you need to use a TypeConverter . See my answer on how to use type converter .

Your complete arraylist of string will be stored as single string after using type converter . You can then write a query to check if the string contains a substring .

Manohar
  • 22,116
  • 9
  • 108
  • 144