我可以执行与ContentResolver.query这款Android查询()? (LEFT JOIN和CASE)这款、query、ContentResolver、Android

2023-09-13 23:40:28 作者:无望的纠缠

我期待在Android上执行以下查询(在伪code):

I am looking to perform the following query (in pseudo-code) on Android:

SELECT C.ID, C.NAME, CASE ISNULL(G.GROUPID,0) = 0 THEN 0 ELSE 1 END INGROUP
FROM CONTACTS C 
LEFT JOIN GROUPMEMBERSHIP G ON G.CONTACTID = C.ID AND G.GROUPID = ?

我期待选择系统中的地址簿中的所有联系人标识和名称,通过默认的联系人的ContentProvider,伴随着 0/1字段,指示该联系人是否为组的成员

I am looking to select the ID and Name of ALL contacts in the system address book, via the default Contacts ContentProvider, along with a 0/1 field indicating whether the contact is a member of group ? .

我当然可以得到所有的联系人很轻松了,然后通过循环和查询的成员分别在我的适配器类很容易,但我想像执行这两个查询为一体的外部连接的查询会产生更好的性能。

I could of course get all contacts easily enough, then loop through and query the membership separately easy enough in my Adapter class, but I'd imagine performing the two queries as one outer joined query would yield much better performance.

我能做到这一点与标准的高级串投影和 ContentResolver.query()的方法?或者将这种查询需要挖掘到更直接的SQL执行?

Can I do this with the standard high-level string-projection and ContentResolver.query() method? Or would this kind of query require digging into more direct SQL execution?

推荐答案

不,你不能做那种与 ContentResolver.query()方法查询。 你需要这样写:

Nope, you can't do that kind of queries with the ContentResolver.query() method. You will need to write something like this:

SQLiteDatabase db = YourActivity.getDbHelper().getReadableDatabase();
String query = yourLongQuery;
Cursor c = db.rawQuery(query, null);
YourActivity.startManagingCursor(c);
c.setNotificationUri(YourActivity.getContentResolver(), YourContentProvider.CONTENT_URI);