android 从列中具有逗号分隔值的Sqllite中获取数据

yfwxisqw  于 2024-01-04  发布在  Android
关注(0)|答案(2)|浏览(152)

要求很简单,我需要得到的行正好有'4'在它,但问题是,在Sqllite没有FIND_IN_SET函数。我试图得到的值使用LIKE查询:
我尝试过的代码片段

  1. select * from tbl_opportunity where (',' || opp_Business_id || ',') LIKE ',4,%'

字符串
但它只返回2行,如果行中包含“24”或任何具有类似“4”的数据的数字,


的数据

px9o7tmv

px9o7tmv1#

SQLite确实支持正则表达式WHERE opp_Business_id REGEXP "\b" || 4 || "\b";-否则你需要匹配所有可能的组合。当它失败2/3的示例记录时,你的模式不匹配应该是显而易见的; WHERE (opp_Business_id == '4' OR opp_Business_id LIKE '%,4' OR opp_Business_id LIKE '4,%' OR opp_Business_id LIKE '%,4,%');将与LIKE进行四次比较-而REGEXP只有一次比较。

2uluyalo

2uluyalo2#

这个问题是关于Android平台的,Android's Sqlite不支持REGEXP关键字。你必须这样写你的查询[为了更好地理解,我使用了Room语法]:

  1. @Query(
  2. "SELECT * FROM tbl_opportunity WHERE " +
  3. "(" +
  4. "',' || opp_Business_id || ',' LIKE '%' || :businessId || ',%' OR " +
  5. "',' || opp_Business_id || ',' LIKE '%' || :businessId OR " +
  6. "',' || opp_Business_id || ',' LIKE :businessId || '%'" +
  7. ") "
  8. )
  9. suspend fun getByBusinessId(businessId: Int): List<Opportunity>

字符串

相关问题