下午研究了很久都没有找到如何在managedQuery方法里面加入group by 条件最后灵机一动!
想出了一个及其猥琐的方法解决此问题!
此时我的需求是查出Calllog中的号码!相同的自然只出一个结果!
正常的查询是这样的!
Cursor phoneCursor = this.managedQuery(CallLog.Calls.CONTENT_URI , new String[] { CallLog.Calls.NUMBER, CallLog.Calls.CACHED_NAME, CallLog.Calls.TYPE }, null, null, CallLog.Calls.DEFAULT_SORT_ORDER); phoneCursor.moveToFirst(); while (phoneCursor.getPosition() != phoneCursor.getCount()) { String number = phoneCursor.getString(phoneCursor .getColumnIndexOrThrow(CallLog.Calls.NUMBER)); String name = phoneCursor.getString(phoneCursor .getColumnIndexOrThrow(CallLog.Calls.CACHED_NAME)); Log.e(TAG, "name:" + name + "number:" + number); phoneCursor.moveToNext(); }
此方法达不到过滤号码相同的!
于是首先想到的要过滤这个最好只在查询的时候就做查出结果后在遍历肯定效率存在问题!
但managedQuery貌似没有提供group by的入口!
首先想到的是在where 子句的入口
Cursor phoneCursor = this.managedQuery(CallLog.Calls.CONTENT_URI , new String[] { CallLog.Calls.NUMBER, CallLog.Calls.CACHED_NAME, CallLog.Calls.TYPE }, "group by CallLog.Calls.NUMBER", null, CallLog.Calls.DEFAULT_SORT_ORDER);
错误出现了
01-08 09:08:25.380: ERROR/DatabaseUtils(334): Writing exception to parcel 01-08 09:08:25.380: ERROR/DatabaseUtils(334): android.database.sqlite.SQLiteException: near "group": syntax error: , while compiling: SELECT number, name, type FROM calls WHERE (group by CallLog.Calls.NUMBER) ORDER BY date DESC 01-08 09:08:25.380: ERROR/DatabaseUtils(334): at android.database.sqlite.SQLiteCompiledSql.native_compile(Native Method) 01-08 09:08:25.380: ERROR/DatabaseUtils(334): at android.database.sqlite.SQLiteCompiledSql.compile(SQLiteCompiledSql.java:91) 01-08 09:08:25.380: ERROR/DatabaseUtils(334): at android.database.sqlite.SQLiteCompiledSql.<init>(SQLiteCompiledSql.java:64) 01-08 09:08:25.380: ERROR/DatabaseUtils(334): at android.database.sqlite.SQLiteProgram.<init>(SQLiteProgram.java:80) 01-08 09:08:25.380: ERROR/DatabaseUtils(334): at android.database.sqlite.SQLiteQuery.<init>(SQLiteQuery.java:46) 01-08 09:08:25.380: ERROR/DatabaseUtils(334): at android.database.sqlite.SQLiteDirectCursorDriver.query(SQLiteDirectCursorDriver.java:42) 01-08 09:08:25.380: ERROR/DatabaseUtils(334): at android.database.sqlite.SQLiteDatabase.rawQueryWithFactory(SQLiteDatabase.java:1345) 01-08 09:08:25.380: ERROR/DatabaseUtils(334): at android.database.sqlite.SQLiteQueryBuilder.query(SQLiteQueryBuilder.java:330) 01-08 09:08:25.380: ERROR/DatabaseUtils(334): at com.android.providers.contacts.CallLogProvider.query(CallLogProvider.java:130) 01-08 09:08:25.380: ERROR/DatabaseUtils(334): at android.content.ContentProvider$Transport.bulkQuery(ContentProvider.java:150) 01-08 09:08:25.380: ERROR/DatabaseUtils(334): at android.content.ContentProviderNative.onTransact(ContentProviderNative.java:111) 01-08 09:08:25.380: ERROR/DatabaseUtils(334): at android.os.Binder.execTransact(Binder.java:288) 01-08 09:08:25.380: ERROR/DatabaseUtils(334): at dalvik.system.NativeStart.run(Native Method) 01-08 09:08:25.390: WARN/dalvikvm(1347): threadid=1: thread exiting with uncaught exception (group=0x40020800) 01-08 09:08:25.390: ERROR/AndroidRuntime(1347): FATAL EXCEPTION: main 01-08 09:08:25.390: ERROR/AndroidRuntime(1347): android.database.sqlite.SQLiteException: near "group": syntax error: , while compiling: SELECT number, name, type FROM calls WHERE (group by CallLog.Calls.NUMBER) ORDER BY date DESC 01-08 09:08:25.390: ERROR/AndroidRuntime(1347): at android.database.DatabaseUtils.readExceptionFromParcel(DatabaseUtils.java:158) 01-08 09:08:25.390: ERROR/AndroidRuntime(1347): at android.database.DatabaseUtils.readExceptionFromParcel(DatabaseUtils.java:114) 01-08 09:08:25.390: ERROR/AndroidRuntime(1347): at android.content.ContentProviderProxy.bulkQueryInternal(ContentProviderNative.java:330) 01-08 09:08:25.390: ERROR/AndroidRuntime(1347): at android.content.ContentProviderProxy.query(ContentProviderNative.java:366) 01-08 09:08:25.390: ERROR/AndroidRuntime(1347): at android.content.ContentResolver.query(ContentResolver.java:245) 01-08 09:08:25.390: ERROR/AndroidRuntime(1347): at android.app.Activity.managedQuery(Activity.java:1520) 01-08 09:08:25.390: ERROR/AndroidRuntime(1347): at com.test.CopyOfTests.onClick(CopyOfTests.java:68) 01-08 09:08:25.390: ERROR/AndroidRuntime(1347): at android.view.View.performClick(View.java:2408) 01-08 09:08:25.390: ERROR/AndroidRuntime(1347): at android.view.View$PerformClick.run(View.java:8816) 01-08 09:08:25.390: ERROR/AndroidRuntime(1347): at android.os.Handler.handleCallback(Handler.java:587) 01-08 09:08:25.390: ERROR/AndroidRuntime(1347): at android.os.Handler.dispatchMessage(Handler.java:92) 01-08 09:08:25.390: ERROR/AndroidRuntime(1347): at android.os.Looper.loop(Looper.java:123) 01-08 09:08:25.390: ERROR/AndroidRuntime(1347): at android.app.ActivityThread.main(ActivityThread.java:4627) 01-08 09:08:25.390: ERROR/AndroidRuntime(1347): at java.lang.reflect.Method.invokeNative(Native Method) 01-08 09:08:25.390: ERROR/AndroidRuntime(1347): at java.lang.reflect.Method.invoke(Method.java:521) 01-08 09:08:25.390: ERROR/AndroidRuntime(1347): at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:876) 01-08 09:08:25.390: ERROR/AndroidRuntime(1347): at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:634) 01-08 09:08:25.390: ERROR/AndroidRuntime(1347): at dalvik.system.NativeStart.main(Native Method)
看到这个错误后我很猥琐的想到了黑客的方法
现在的查询语句是
SELECT number, name, type FROM calls WHERE (group by CallLog.Calls.NUMBER) ORDER BY date DESC
吧它改装一下
SELECT number, name, type FROM calls WHERE (1=1)group by CallLog.Calls.NUMBER () ORDER BY date DESC
后面那个()会出现错误那我一不做二不休
SELECT number, name, type FROM calls WHERE (1=1) group by number --() ORDER BY date DESC
于是我成了的加入了group by查询
Cursor phoneCursor = this.managedQuery(CallLog.Calls.CONTENT_URI , new String[] { CallLog.Calls.NUMBER, CallLog.Calls.CACHED_NAME, CallLog.Calls.TYPE }, "1=1) group by "+CallLog.Calls.NUMBER+" -- (", null, CallLog.Calls.DEFAULT_SORT_ORDER);
时间: 2024-10-24 08:23:14