dan*_*wag 5 sqlite android android-sqlite android-room
我在“谷歌搜索”这个时遇到了麻烦,但希望它对专家(甚至中级)来说不是太棘手。
有没有办法在未知数量的多个 OR 条件中搜索子字符串?
Android SQLite Room Persistence DAO 的原始查询是:
SELECT country FROM table WHERE country IN (:searchList)
Run Code Online (Sandbox Code Playgroud)
其中,给定 ['Paris, France', 'Berlin, Germany'] 的 searchList 将转换为 SQL:
SELECT country FROM table WHERE country IN ( 'Paris, France', 'Berlin, Germany' );
Run Code Online (Sandbox Code Playgroud)
并且会返回:['巴黎,法国','柏林,德国']
但是,我希望能够仅按国家/地区进行搜索。例如,给定 ['France', 'Germany'] 的搜索列表,不幸的是我没有得到任何结果。
我知道以下 SQL 可以解决问题:
SELECT country FROM table WHERE country LIKE '%France%' OR country LIKE '%Germany%';
Run Code Online (Sandbox Code Playgroud)
但是,因为我不知道 searchList 中有多少元素,所以我必须使用原始格式:
SELECT country FROM table WHERE country IN (:searchList)
Run Code Online (Sandbox Code Playgroud)
有什么方法可以使上述搜索国家/地区列中的子字符串?
非常感谢您的帮助,
担
由于 Android Sqlite 未附带 Regex,因此您需要在搜索之前拆分列表。
SQLite IN-Operator 对列表进行排序并使用二分搜索,在这种情况下,这比使用单个查询要快得多。
性能 OR 与 IN 比较速度(不幸的是不适用于批量查询)
SELECT COUNT(*) FROM t_inner WHERE val IN (1000, 2000, 3000, 4000, 5000, 6000, 7000, 8000, 9000);
1 row fetched in 0.0032 (1.2679 seconds)
SELECT COUNT(*) FROM t_inner WHERE val = 1000 OR val = 2000 OR val = 3000 OR val = 4000 OR val = 5000 OR val = 6000 OR val = 7000 OR val = 8000 OR val = 9000;
1 row fetched in 0.0026 (1.7385 seconds)
Run Code Online (Sandbox Code Playgroud)
| 归档时间: |
|
| 查看次数: |
1783 次 |
| 最近记录: |