我正在尝试进行查询,以使字段与字符串列表匹配。该字段最初是一个List,通过TypeConverter保存为字符串。 如果我将此字段与LIKE子句匹配,它将起作用:
val sql = SimpleSQLiteQuery(
"""
SELECT * FROM practice
WHERE categories LIKE '%advaita%'
ORDER BY id $order LIMIT $limit
"""
)
return searchPracticesByCategory(sql)
但是,如果我尝试使用IN子句传递字符串列表,那么我将没有结果:
val sql = SimpleSQLiteQuery(
"""
SELECT * FROM practice
WHERE categories IN ('%advaita%', '%meditation%')
ORDER BY id $order LIMIT $limit
"""
)
return searchPracticesByCategory(sql)
在Dao中,我对查询进行了如下注释:
@RawQuery(observedEntities = [Practice::class])
abstract fun searchPracticesByCategory(query: SupportSQLiteQuery): Observable<List<Practice>>
字段:
@TypeConverters(ListConverter::class)
val categories: List<String>? = null
和TypeConverter:
class ListConverter: Serializable {
@TypeConverter
fun restoreList(listOfString: String?): List<String>? {
return Gson().fromJson(
listOfString,
object : TypeToken<List<String>?>() {}.getType()
)
}
@TypeConverter
fun saveList(listOfString: List<String>?): String? {
return Gson().toJson(listOfString)
}
}
知道可能是什么问题吗?