冰淇淋三明治中 ContentResolver 中的 Group By
我正在对 Android Contacts ContentProvider 进行查询。我需要一个 Group By 子句。在 Gingerbread 和 Honeycomb 中,我执行类似的操作来同时搜索电话号码和电子邮件:(
实际的 WHERE 子句要复杂得多,因为它包括类型检查。这是一种简化,但会产生相同的结果)
String request = Phone.NUMBER + " LIKE ? OR " + Email.DATA + " LIKE ?";
String[] params = new String["%test%", "%test%"];
Cursor cursor = getContentResolver().query(
Data.CONTENT_URI,
new String[] { Data._ID, Data.RAW_CONTACT_ID },
request + ") GROUP BY (" + Data.RAW_CONTACT_ID,
params, "lower(" + Data.DISPLAY_NAME + ") ASC");
注入')' 结束 WHERE 子句并允许插入 GROUP BY 子句。
然而,在 Ice Cream Sandwich 中,ContentProvider 似乎检测到了这一点并添加了正确数量的括号以防止我的注入。在单个游标查询中还有其他方法可以做到这一点吗?
编辑
目前,我已经删除了 GROUP BY,并添加了 MatrixCursor 以限制影响,但我宁愿有一个真正的光标:
MatrixCursor result = new MatrixCursor(new String[] { Data._ID, Data.RAW_CONTACT_ID });
Set<Long> seen = new HashSet<Long>();
while (cursor.moveToNext()) {
long raw = cursor.getLong(1);
if (!seen.contains(raw)) {
seen.add(raw);
result.addRow(new Object[] {cursor.getLong(0), raw});
}
}
I am making a query on the Android Contacts ContentProvider. I need a Group By clause. In Gingerbread and Honeycomb, I do something like this to search phone numbers and emails at the same time:
(The actual WHERE clause is much more complicated as it includes types checks. This is a simplification, but it yields the same result)
String request = Phone.NUMBER + " LIKE ? OR " + Email.DATA + " LIKE ?";
String[] params = new String["%test%", "%test%"];
Cursor cursor = getContentResolver().query(
Data.CONTENT_URI,
new String[] { Data._ID, Data.RAW_CONTACT_ID },
request + ") GROUP BY (" + Data.RAW_CONTACT_ID,
params, "lower(" + Data.DISPLAY_NAME + ") ASC");
The injection of the ')' finishes the WHERE clause and allow the insertion of a GROUP BY clause.
However, in Ice Cream Sandwich, it appears that the ContentProvider detects this and adds the correct number of parenthesis to prevent my injection. Any other way of doing this in a single cursor query?
Edit
Currently, I have removed the GROUP BY, and added a MatrixCursor to limit the impact, but I'd rather have a real cursor:
MatrixCursor result = new MatrixCursor(new String[] { Data._ID, Data.RAW_CONTACT_ID });
Set<Long> seen = new HashSet<Long>();
while (cursor.moveToNext()) {
long raw = cursor.getLong(1);
if (!seen.contains(raw)) {
seen.add(raw);
result.addRow(new Object[] {cursor.getLong(0), raw});
}
}
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(3)
我最近在查询 CallLog.Calls DB 时遇到了这个问题(我们无法修改 ContentProvider)。我们最终要构建一个如下所示的查询:
这里的想法是,我们将任何有效的 sqlite 放入子查询中,返回 id 列表,然后再次查询具有这些 id 的所有调用。
最终的代码看起来像这样:
在您查询联系人的情况下,它看起来像这样:
性能会有所下降(因为我们本质上是为相同的结果查询两次),但它会肯定比在 Java 世界中查询所有调用和执行 GROUP BY 工作要快得多,并且还允许您使用附加子句构建查询。
希望这有帮助。我们在奥利奥上使用了它,它满足了我们的需求。
I recently battled this issue querying the CallLog.Calls DB (where we were not able to modify the ContentProvider). What we ended up going with was building a query that looked like this:
The idea here is that we place any valid sqlite in our subquery, return a list of ids and then query again for all calls with those ids.
The final code looked something like this:
In the case that you're querying for contacts, it would look something like this:
There will be some decrease in performance (since we're essentially querying twice for the same results), but it will surely be a lot faster than querying for all calls and doing the
GROUP BY
work in java world and also allows you to build up the query with additional clauses.Hope this helps. We used this on Oreo and it fulfilled our needs.
您可以创建一个自定义
Uri
,这样当ContentProvider
中的UriMatcher
获取它时,您可以插入 group by 子句,然后执行原始代码直接sql写入数据库。You could create a custom
Uri
such that when yourUriMatcher
in yourContentProvider
gets it, you can insert your group by clause and then execute the raw sql directly on the database.首先请原谅我糟糕的英语!
我是 Java/Android 新手,从 4.2.1 开始,并与之斗争了近 2 天,然后我开始阅读有关 SQLiteQueryBuilder query 部分几乎就是您正在寻找的内容;)
它具有:
内容提供程序的查询“功能”只为您提供:
在这里您可以欺骗,我将向您发布我的代码片段:
就是这样!
这是我用来执行的代码:
first off all excuse my POOR English!
I'm new to Java/Android, started with 4.2.1 and fight with that too almost 2 days, then i start reading some more details about SQLiteQueryBuilder the query part is pretty much that what u are looking for ;)
it have:
the query "function" of the Content Provider only gives you:
here u can trick around, i will post you my code snip:
thats its!
here the code i use to execute: