ContentResolver.query()—>buildQueryString()
Cursor cursor = context.getContentResolver().query(Sms.CONTENT_URI,
new String[]{"thread_id from sms where type = 3 group by thread_id—"}, // 可以这样使用。
null, null, null);
07-17 10:55:17.084: E/AndroidRuntime(30157): FATAL EXCEPTION: main
07-17 10:55:17.084: E/AndroidRuntime(30157): java.lang.NullPointerException
07-17 10:55:17.084: E/AndroidRuntime(30157): at com.txrj.sms.activity.ConversationListActivity$LoadThreadsExtra.run(ConversationListActivity.java:181)
07-17 10:55:17.084: E/AndroidRuntime(30157): at android.os.Handler.handleCallback(Handler.java:605)
07-17 10:55:17.084: E/AndroidRuntime(30157): at android.os.Handler.dispatchMessage(Handler.java:92)
07-17 10:55:17.084: E/AndroidRuntime(30157): at android.os.Looper.loop(Looper.java:137)
07-17 10:55:17.084: E/AndroidRuntime(30157): at android.app.ActivityThread.main(ActivityThread.java:4517)
07-17 10:55:17.084: E/AndroidRuntime(30157): at java.lang.reflect.Method.invokeNative(Native Method)
07-17 10:55:17.084: E/AndroidRuntime(30157): at java.lang.reflect.Method.invoke(Method.java:511)
07-17 10:55:17.084: E/AndroidRuntime(30157): at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:993)
07-17 10:55:17.084: E/AndroidRuntime(30157): at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:760)
07-17 10:55:17.084: E/AndroidRuntime(30157): at dalvik.system.NativeStart.main(Native Method)
Cursor cursor = context.getContentResolver().query(Sms.CONTENT_URI,
new String[]{"thread_id"},
"type = 3 group by thread_id", null, null); // 不可以这样使用。
07-17 11:16:48.988: E/AndroidRuntime(32627): FATAL EXCEPTION: main
07-17 11:16:48.988: E/AndroidRuntime(32627): android.database.sqlite.SQLiteException: near "group": syntax error: , while compiling: SELECT thread_id FROM sms WHERE (type = 3 group by thread_id) ORDER BY date DESC
07-17 11:16:48.988: E/AndroidRuntime(32627): at android.database.DatabaseUtils.readExceptionFromParcel(DatabaseUtils.java:180)
07-17 11:16:48.988: E/AndroidRuntime(32627): at android.database.DatabaseUtils.readExceptionFromParcel(DatabaseUtils.java:136)
07-17 11:16:48.988: E/AndroidRuntime(32627): at android.content.ContentProviderProxy.query(ContentProviderNative.java:358)
07-17 11:16:48.988: E/AndroidRuntime(32627): at android.content.ContentResolver.query(ContentResolver.java:311)
07-17 11:16:48.988: E/AndroidRuntime(32627): at com.txrj.sms.manager.SmsDataManager.getThreadsWithDraftMsg(SmsDataManager.java:33)
07-17 11:16:48.988: E/AndroidRuntime(32627): at com.txrj.sms.activity.ConversationListActivity$LoadThreadsExtra.run(ConversationListActivity.java:170)
07-17 11:16:48.988: E/AndroidRuntime(32627): at android.os.Handler.handleCallback(Handler.java:605)
07-17 11:16:48.988: E/AndroidRuntime(32627): at android.os.Handler.dispatchMessage(Handler.java:92)
07-17 11:16:48.988: E/AndroidRuntime(32627): at android.os.Looper.loop(Looper.java:137)
07-17 11:16:48.988: E/AndroidRuntime(32627): at android.app.ActivityThread.main(ActivityThread.java:4517)
07-17 11:16:48.988: E/AndroidRuntime(32627): at java.lang.reflect.Method.invokeNative(Native Method)
07-17 11:16:48.988: E/AndroidRuntime(32627): at java.lang.reflect.Method.invoke(Method.java:511)
07-17 11:16:48.988: E/AndroidRuntime(32627): at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:993)
07-17 11:16:48.988: E/AndroidRuntime(32627): at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:760)
07-17 11:16:48.988: E/AndroidRuntime(32627): at dalvik.system.NativeStart.main(Native Method)
Cursor cursor = context.getContentResolver().query(Sms.CONTENT_URI,
new String[]{"thread_id"},
"type = 3", null, "thread_id desc group by thread_id"); // 不可以这样使用。
07-17 11:20:57.040: E/AndroidRuntime(940): FATAL EXCEPTION: main
07-17 11:20:57.040: E/AndroidRuntime(940): android.database.sqlite.SQLiteException: near "group": syntax error: , while compiling: SELECT thread_id FROM sms WHERE (type = 3) ORDER BY thread_id desc group by thread_id
07-17 11:20:57.040: E/AndroidRuntime(940): at android.database.DatabaseUtils.readExceptionFromParcel(DatabaseUtils.java:180)
07-17 11:20:57.040: E/AndroidRuntime(940): at android.database.DatabaseUtils.readExceptionFromParcel(DatabaseUtils.java:136)
07-17 11:20:57.040: E/AndroidRuntime(940): at android.content.ContentProviderProxy.query(ContentProviderNative.java:358)
07-17 11:20:57.040: E/AndroidRuntime(940): at android.content.ContentResolver.query(ContentResolver.java:311)
07-17 11:20:57.040: E/AndroidRuntime(940): at com.txrj.sms.manager.SmsDataManager.getThreadsWithDraftMsg(SmsDataManager.java:33)
07-17 11:20:57.040: E/AndroidRuntime(940): at com.txrj.sms.activity.ConversationListActivity$LoadThreadsExtra.run(ConversationListActivity.java:170)
07-17 11:20:57.040: E/AndroidRuntime(940): at android.os.Handler.handleCallback(Handler.java:605)
07-17 11:20:57.040: E/AndroidRuntime(940): at android.os.Handler.dispatchMessage(Handler.java:92)
07-17 11:20:57.040: E/AndroidRuntime(940): at android.os.Looper.loop(Looper.java:137)
07-17 11:20:57.040: E/AndroidRuntime(940): at android.app.ActivityThread.main(ActivityThread.java:4517)
07-17 11:20:57.040: E/AndroidRuntime(940): at java.lang.reflect.Method.invokeNative(Native Method)
07-17 11:20:57.040: E/AndroidRuntime(940): at java.lang.reflect.Method.invoke(Method.java:511)
07-17 11:20:57.040: E/AndroidRuntime(940): at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:993)
07-17 11:20:57.040: E/AndroidRuntime(940): at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:760)
07-17 11:20:57.040: E/AndroidRuntime(940): at dalvik.system.NativeStart.main(Native Method)
com.android.providers.telephony.SmsProvider.query(Uri, String[], String, String[], String)
SQLiteDatabase db = mOpenHelper.getReadableDatabase();
Cursor ret = qb.query(db, projectionIn, selection, selectionArgs,
null, null, orderBy);
-->
android.database.sqlite.SQLiteQueryBuilder.query(SQLiteDatabase, String[], String, String[], String, String, String)
public Cursor query(SQLiteDatabase db, String[] projectionIn,
String selection, String[] selectionArgs, String groupBy,
String having, String sortOrder) {
return query(db, projectionIn, selection, selectionArgs, groupBy, having, sortOrder,
null /* limit */);
}
-->
android.database.sqlite.SQLiteQueryBuilder.query(SQLiteDatabase, String[], String, String[], String, String, String, String)
public Cursor query(SQLiteDatabase db, String[] projectionIn,
String selection, String[] selectionArgs, String groupBy,
String having, String sortOrder, String limit) {
if (mTables == null) {
return null;
}
String sql = buildQuery(
projectionIn, selection, selectionArgs, groupBy, having,
sortOrder, limit);
if (Log.isLoggable(TAG, Log.DEBUG)) {
Log.d(TAG, "Performing query: " + sql);
}
return db.rawQueryWithFactory(
mFactory, sql, selectionArgs,
SQLiteDatabase.findEditTable(mTables));
}
-->
android.database.sqlite.SQLiteQueryBuilder.buildQuery(String[], String, String[], String, String, String, String)
public String buildQuery(
String[] projectionIn, String selection, String[] selectionArgs,
String groupBy, String having, String sortOrder, String limit) {
String[] projection = computeProjection(projectionIn);
StringBuilder where = new StringBuilder();
boolean hasBaseWhereClause = mWhereClause != null && mWhereClause.length() > 0;
if (hasBaseWhereClause) {
where.append(mWhereClause.toString());
where.append(')');
}
// Tack on the user's selection, if present.
if (selection != null && selection.length() > 0) {
if (hasBaseWhereClause) {
where.append(" AND ");
}
where.append('(');
where.append(selection);
where.append(')');
}
return buildQueryString(
mDistinct, mTables, projection, where.toString(),
groupBy, having, sortOrder, limit);
}
-->
android.database.sqlite.SQLiteQueryBuilder.buildQueryString(boolean, String, String[], String, String, String, String, String)
public static String buildQueryString(
boolean distinct, String tables, String[] columns, String where,
String groupBy, String having, String orderBy, String limit) {
if (TextUtils.isEmpty(groupBy) && !TextUtils.isEmpty(having)) {
throw new IllegalArgumentException(
"HAVING clauses are only permitted when using a groupBy clause");
}
if (!TextUtils.isEmpty(limit) && !sLimitPattern.matcher(limit).matches()) {
throw new IllegalArgumentException("invalid LIMIT clauses:" + limit);
}
StringBuilder query = new StringBuilder(120);
query.append("SELECT ");
if (distinct) {
query.append("DISTINCT ");
}
if (columns != null && columns.length != 0) {
appendColumns(query, columns);
} else {
query.append("* ");
}
query.append("FROM ");
query.append(tables);
appendClause(query, " WHERE ", where);
appendClause(query, " GROUP BY ", groupBy);
appendClause(query, " HAVING ", having);
appendClause(query, " ORDER BY ", orderBy);
appendClause(query, " LIMIT ", limit);
return query.toString();
}
-->
private static void appendClause(StringBuilder s, String name, String clause) {
if (!TextUtils.isEmpty(clause)) {
s.append(name);
s.append(clause);
}
}
class LoadThreadsExtra implements Runnable {
List<Integer> draftIds = null;
List<Integer> failIds = null;
Map<Integer, Integer> unreadMap = null;
Map<Integer, String> canonicalAddrMap = null;
Map<String, String> nameAddrMap = null;
@Override
public void run() {
draftIds = SmsDataManager.getThreadsWithDraftMsg(mContext);
failIds = SmsDataManager.getThreadsWithFailedMsg(mContext);
unreadMap = SmsDataManager.getThreadUnreadCountMap(mContext);
canonicalAddrMap = SmsDataManager.getCanonicalAddressMap(mContext);
nameAddrMap = SmsDataManager.getNameAddressMap(mContext);
int threadCount = mThreads.size();
for(int i=0;i<threadCount;i++){
TxrjThreads thread = mThreads.get(i);
long threadId = thread.getThreadId();
thread.setHasDraftMsg(draftIds.contains(threadId)); // draftIds是Integer类型的列表,而threadId是long类型。决定不可能返回true!
thread.setHasFailMsg(failIds.contains(threadId)); // 同上
thread.setUnReadCount(unreadMap.containsKey(threadId) ? unreadMap.get(threadId) : 0); // 同上
Log.i("txrjsms", "threadId:"+threadId+", hasDraftMsg:"+thread.isHasDraftMsg()
+", hasFailMsg:"+thread.isHasFailMsg()+", unread:"+thread.getUnReadCount());
}
mHandler.sendEmptyMessage(TxrjConstant.WHAT_NOTIFY_DATA_CHANGED);
}
}
07-17 11:58:20.379: I/txrjsms(2402): show data in listview.
07-17 11:58:21.470: I/txrjsms(2402): threadId:455, hasDraftMsg:false, hasFailMsg:true, unread:1
07-17 11:58:21.470: I/txrjsms(2402): threadId:459, hasDraftMsg:true, hasFailMsg:true, unread:0
07-17 11:58:21.470: I/txrjsms(2402): threadId:457, hasDraftMsg:false, hasFailMsg:false, unread:0
07-17 11:58:21.470: I/txrjsms(2402): threadId:458, hasDraftMsg:false, hasFailMsg:false, unread:0
07-17 11:58:21.470: I/txrjsms(2402): threadId:456, hasDraftMsg:false, hasFailMsg:false, unread:0
点击某个会话跳转到信息列表界面,没有显示任何信息出来。
查看代码定位到产生bug的原因是getIntent().getIntExtra()无法获取到long类型的threadId。
ConversationListActivity.java
it.putExtra(TxrjConstant.EXTRA_THREAD_ID, thread.getThreadId());
MessageListActivity.java
mThreadId = getIntent().getIntExtra(TxrjConstant.EXTRA_THREAD_ID, -1);
将getIntExtra改成getLongExtra。如下。
mThreadId = getIntent().getLongExtra(TxrjConstant.EXTRA_THREAD_ID, -1);
ContentResolver.query()—>buildQueryString()的更多相关文章
- Android:联系人Contacts之ContentResolver query 参数详解
注:本片整理自 http://blog.csdn.net/wssiqi/article/details/8132603 1.获取联系人姓名 一个简单的例子,这个函数获取设备上所有的联系人ID和联系人N ...
- [ 原创 ]学习笔记-Android 学习笔记 Contacts (一)ContentResolver query 参数详解 [转载]
此博文转载自:http://blog.csdn.net/wssiqi/article/details/8132603 1.获取联系人姓名 一个简单的例子,这个函数获取设备上所有的联系人ID和联系人NA ...
- ContentProvider官方教程(3)ContentResolver查询、遍历 示例
Retrieving Data from the Provider This section describes how to retrieve data from a provider, using ...
- 使用ContentResolver添加数据、查询数据
import java.util.ArrayList;import java.util.HashMap;import java.util.Map; import android.os.Bundle;i ...
- ContentProvider与ContentResolver使用【转】
这篇文章被转载而转载者未注明原文出处,在此未加上原文地址链接,本人向原作者致以歉意. 下面是文章内容: 使用ContentProvider共享数据: 当应用继承ContentProvider类,并重写 ...
- managedQuery和query的区别,
我们都知道在Android系统中,SQLite数据库的相关操作方式被封装为内容提供Content Provider,可以帮助那些不会SQL语言的开发者快速实现Android平台上的数据库操作,但是平时 ...
- ContentProvider与ContentResolver使用
例如以下内容为从网络转载: 使用ContentProvider共享数据: 当应用继承ContentProvider类,并重写该类用于提供数据和存储数据的方法,就能够向其它应用共享其数据.虽然使用其它方 ...
- ContentResolver + SqliteOpenHelper + ContentProvider 理解
惭愧,现在才接触到ContentResolver的用法 这个类主要是Android用来实现应用程序之间数据共享的 一个应用程序可以将自己的数据完全暴露出去,外界更本看不到,也不用看到这个应用程序暴露的 ...
- android利用ContentResolver访问者获取手机联系人信息
转载自:http://www.jb51.net/article/106379.htm 首先需要在AndroidManifest.xml文件中添加权限: <uses-permission andr ...
随机推荐
- N体运动的程序模拟
这依然是与<三体>有关的一篇文章.空间中三个星体在万有引力作用下的运动被称之为三体问题,参见我的上一篇文章:三体运动的程序模拟.而这一节,对三体问题进行了扩展,实现了空间中N个星体在万有引 ...
- IIS7.5中调试.Net 4.0网站出现无厘头500错误的解决办法 (转)
刚刚 部署了ii7的dll的有x86写的,就会出现以下这样的问题 iis 7 x86,Could not load file or assembly 'Name' or one of its depe ...
- [leetcode]Max Points on a Line @ Python
原题地址:https://oj.leetcode.com/problems/max-points-on-a-line/ 题意:Given n points on a 2D plane, find th ...
- 如何在Vblock里配置Boot from SAN
啥是vBlock ============ vBlock是VCE用在包含了它的数据中心产品的组件的机架上的一个商标名. 机架中的组件都是有VCE出厂前预先组装好的, 组件的预设以及解决方案, 都是客户 ...
- 谷歌(Google)被墙,解决地图和字体无法显示的问题
首先,本文以及本站所有文章都是技术探讨文章,不鼓励任何人去fan qiang以及做任何违法的事情.接下来是正文: 谷歌基本上是被和谐透了,谷歌地图API自然也打不开了,于是公司网站上那些谷歌地图都变成 ...
- 理解TensorFlow的Queue
https://www.jianshu.com/p/d063804fb272 这篇文章来说说TensorFlow里与Queue有关的概念和用法. 其实概念只有三个: Queue是TF队列和缓存机制的实 ...
- Git直接拉取远程分支
用Git,一直有个疑惑,可不可以不拉取远程Origin主干,我直接pull一个分支下来 今天想了一下,找到了一个办法 本地分支关联 // 0.新建一个文件夹,然后初始化git git init // ...
- mybatis 是如何与数据表对应上的 ?
MyBatis也需要进行表和实体 的关联.我们查询的是表,返回的结果是实体类.这之间有一个对应关系. 如果说实体类的属性和表的列名一一对应,名字一样,那就自动解决了这个问题.但是如果实体类的属性和表的 ...
- $#65279导致页面body会出现一个空白行
模板文件生成html文件之后会在body开头处加入一个可见的控制符,导致页面头部会出现一个空白行.原因是页面的编码是UTF-8 + BOM. 直接notopad++ 保存为无BO ...
- “服务器推”技术【转载+整理】
原文地址 本文内容 "服务器推(server-push)"技术的应用 基于客户端套接口的"服务器推"技术 基于 HTTP 长连接的"服务器推" ...