Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

How do I make a case insensitive query with ContentResolver in Android?

Tags:

android

sqlite

My goal is to get all rows from native db with a specific email address on Android Gingerbread and above.

This query gets only the rows where the case also matches.

Cursor cursor = context.getContentResolver().query(ContactsContract.CommonDataKinds.Email.CONTENT_URI,
            new String[] {Contacts._ID}, ContactsContract.CommonDataKinds.Email.ADDRESS + "=?",
            new String[] {email}, null);

I've read about WHERE clauses to sql where one use lower('xx') but I can't figure out how to use it. Also I learned to you can make the column not care about case when creating the table, but that ship has sailed.

like image 855
jayeffkay Avatar asked Sep 24 '12 13:09

jayeffkay


Video Answer


2 Answers

Also try

Cursor cursor = context.getContentResolver().query(ContactsContract.CommonDataKinds.Email.CONTENT_URI,
            new String[] {Contacts._ID}, ContactsContract.CommonDataKinds.Email.ADDRESS + "=? COLLATE NOCASE",
            new String[] {email}, null);

Note that added COLLATE NOCASE in the query.

like image 69
sampathpremarathna Avatar answered Oct 15 '22 01:10

sampathpremarathna


Please try (I suppose that mail is String object):

Cursor cursor = context.getContentResolver().query(ContactsContract.CommonDataKinds.Email.CONTENT_URI,
        new String[] {Contacts._ID}, "lower("+ContactsContract.CommonDataKinds.Email.ADDRESS + ")=lower('"+ email +"')",
        null, null);

or

Cursor cursor = context.getContentResolver().query(ContactsContract.CommonDataKinds.Email.CONTENT_URI,
        new String[] {Contacts._ID}, "lower("+ContactsContract.CommonDataKinds.Email.ADDRESS + ")=lower('"+ email +"')",
        new String[] {}, null);
like image 23
ra. Avatar answered Oct 15 '22 01:10

ra.