i would like sort the call logs by cached name. The cached name can be null. So in the case of a null cached name, i would like to have the phone number for an alias.

in sqlite, there is the ifnull() function. ifnull details

I try :

String[] projections = new String[] { Calls._ID, Calls.NUMBER, Calls.DATE, Calls.TYPE, Calls.DURATION, "ifnull("+Calls.CACHED_NAME+","+Calls.NUMBER+") as display_name" };

Cursor cursor_call = ctx.getContentResolver().query(URI_CALL_LOGS,
                    projections,
                    null,
                    null,
                    null);

But i have an error with my use of ifnull an i don't find a sample of this function.

java.lang.IllegalArgumentException: Invalid column ifnull(name,number) as display_name

有帮助吗?

解决方案

Because of the way you're creating the query, things like parentheses are being auto-escaped to prevent SQL-injection attacks.

In situations where you have direct access to the raw DB (not through a contentProvider), you can use a SQLiteDatabase.rawQuery, like so:

myDB.rawquery("select * from a, b where a.id=b.someid", null);

But in this case it looks like you want to access the contact app's call_log database, which you can only do through the provided ContentResolver. Since it's intentionally designed to only let you send values and not SQL commands as variables, ifnull(...) isn't going to work, and you'll need to choose between name and number using logic when you're pulling data from the cursor.

许可以下: CC-BY-SA归因
不隶属于 StackOverflow
scroll top