使用getContentResolver()。query()无法执行CASE WHEN查询

时间:2017-06-28 06:07:08

标签: android sqlite android-contacts

我试图在设备的联系人身上执行以下查询:

final String[] PROJECTION = {
        ContactsContract.CommonDataKinds.Phone._ID,
        ContactsContract.CommonDataKinds.Phone.LOOKUP_KEY,
        ContactsContract.CommonDataKinds.Phone.DISPLAY_NAME_PRIMARY,
        ContactsContract.CommonDataKinds.Phone.NUMBER,
        String.format("CASE WHEN " + ContactsContract.CommonDataKinds.Phone.DISPLAY_NAME_PRIMARY + " LIKE '%%%s%%' THEN 'full' END name_match", mFirstName)
};

String SELECTION = ContactsContract.CommonDataKinds.Phone.DISPLAY_NAME_PRIMARY + " LIKE ?";

String[] selectionArgs = {mFirstName};

String ORDER_BY = "name_match ASC";

Cursor cursor = context.getContentResolver().query(ContactsContract.CommonDataKinds.Phone.CONTENT_URI, PROJECTION, SELECTION, selectionArgs, ORDER_BY);

但我得到以下例外:

                                             java.lang.IllegalArgumentException: Invalid column CASE WHEN display_name LIKE '%john%' THEN 'full' END name_match
                                                 at android.database.sqlite.SQLiteQueryBuilder.computeProjection(SQLiteQueryBuilder.java:632)
                                                 at android.database.sqlite.SQLiteQueryBuilder.buildQuery(SQLiteQueryBuilder.java:447)
                                                 at android.database.sqlite.SQLiteQueryBuilder.query(SQLiteQueryBuilder.java:387)
                                                 at com.android.providers.contacts.ContactsProvider2.query(ContactsProvider2.java:7812)
                                                 at com.android.providers.contacts.ContactsProvider2.queryLocal(ContactsProvider2.java:7550)
                                                 at com.android.providers.contacts.ContactsProvider2.query(ContactsProvider2.java:6037)
                                                 at android.content.ContentProvider$Transport.query(ContentProvider.java:238)
                                                 at android.content.ContentProviderNative.onTransact(ContentProviderNative.java:112)
                                                 at android.os.Binder.execTransact(Binder.java:453)

这里有什么问题?是否可以在内容提供商上执行CASE WHEN查询?在联系人表上?如果没有,有没有其他方法可以做这样的查询?

感谢。

1 个答案:

答案 0 :(得分:0)

您可以在代码中轻松完成,而不是在ContentProvider中执行您想要的操作(这似乎是不可能的):

final String[] projection = { ... };
String selection = Phone.DISPLAY_NAME_PRIMARY + " LIKE %?%";
String[] selectionArgs = { mFirstName };
Cursor cursor = cr.query(Phone.CONTENT_URI, projection, selection, selectionArgs, null);
List<String> fullMatch = new ArrayList<>();
List<String> partialMatch = new ArrayList<>();
while (cursor.moveToNext()) {
   String name = cursor.getString(3); // exact number depends on your projection
   if (mFirstName.equalsIgnoreCase(name) {
      fullMatch.add(name);
   } else {
      partialMatch.add(name);
   }
}
cursor.close();
List<String> allMatches = new ArrayList<>();
allMatches.addAll(fullMatch);
allMatches.addAll(partialMatch);