Android Contacts.CONTENT_STREQUENT_URI在Android 4.1.2和4.4.2上抛出SQLiteException

时间:2015-11-30 22:02:55

标签: android android-sqlite android-contacts android-contentresolver contactscontract

我需要一些帮助。

我正在尝试查询Android上的strequent表格(ContactsContract.CONTENT_STREQUENT_URI),以获得加星标和最常联系的联系人。

Cursor cursor = context.getContentResolver().query(Contacts.CONTENT_STREQUENT_URI,
                            new String[]{Contacts._ID, Contacts.LOOKUP_KEY, Contacts.STARRED, Utils.hasHoneycomb() ? Contacts.DISPLAY_NAME_PRIMARY
                                    : Contacts.DISPLAY_NAME}, null,
                            null, null
                    );

在大多数设备和Android版本上,它运行正常。但是在Android 4.1.2版本上我得到了这个SQLiteException

{AsyncTask #2} android.database.sqlite.SQLiteException: near "(": syntax error (code 1): ,
 while compiling: SELECT * FROM (SELECT _id, lookup, starred, display_name, 9223372036854775807
 AS times_used, 9223372036854775807 AS last_time_used FROM view_contacts WHERE 
(view_contacts.[single_is_restricted] = 0(1)) AND (starred=1) GROUP BY _id ORDER 
BY display_name COLLATE LOCALIZED ASC) UNION ALL SELECT * FROM (SELECT _id, 
lookup, starred, display_name, SUM(data_usage_stat.times_used) AS times_used, 
MAX(data_usage_stat.last_time_used) AS last_time_used FROM view_data_usage_stat AS 
data_usage_stat INNER JOIN view_contacts ON ((data_usage_stat.times_used > 0) AND 
(contact_id=view_contacts._id)) WHERE (view_contacts.[single_is_restricted] = 0 
AND ((starred =0 OR starred IS NULL) AND account_type NOT IN 
('com.android.contacts.sim'))) GROUP BY _id HAVING contact_id IN default_directory 
ORDER BY (CASE WHEN (strftime('%s', 'now') - last_time_used/1000) < 259200 THEN 0 
WHEN (strftime('%s', 'now') - last_time_used/1000) < 2592000 THEN 1 ELSE 2 END), 
times_used DESC LIMIT 10) at 
android.database.DatabaseUtils.readExceptionFromParcel(DatabaseUtils.java:184) at 
android.database.DatabaseUtils.readExceptionFromParcel(DatabaseUtils.java:140) at 
android.content.ContentProviderProxy.query(ContentProviderNative.java:366) at 
android.content.ContentResolver.query(ContentResolver.java:371) at 
android.content.ContentResolver.query(ContentResolver.java:314) at 
(...)

我很确定我在Android 4.4.2版本上获得的其他SQLiteException是相关的:

 android.database.sqlite.SQLiteException: near ")": syntax error (code 1): , while 
compiling: SELECT * FROM (SELECT _id, lookup, starred, display_name, 
9223372036854775807 AS times_used, 9223372036854775807 AS last_time_used FROM 
view_contacts WHERE (view_contacts.[single_is_restricted] = 0 AND ) AND 
(starred=1) GROUP BY _id ORDER BY display_name COLLATE LOCALIZED ASC) UNION ALL 
SELECT * FROM (SELECT _id, lookup, starred, display_name, 
SUM(data_usage_stat.times_used) AS times_used, MAX(data_usage_stat.last_time_used) 
AS last_time_used FROM view_data_usage_stat AS data_usage_stat INNER JOIN 
view_contacts ON ((data_usage_stat.times_used > 0) AND 
(contact_id=view_contacts._id)) WHERE (view_contacts.[single_is_restricted] = 
0((starred =0 OR starred IS NULL) AND account_type NOT IN 
('com.android.contacts.sim'))) GROUP BY _id HAVING contact_id IN default_directory 
ORDER BY (CASE WHEN (strftime('%s', 'now') - last_time_used/1000) < 259200 THEN 0 
WHEN (strftime('%s', 'now') - last_time_used/1000) < 604800 THEN 1 WHEN 
(strftime('%s', 'now') - last_time_used/1000) < 1209600 THEN 2 WHEN 
(strftime('%s', 'now') - last_time_used/1000) < 2592000 THEN 3 ELSE 4 END), 
times_used DESC LIMIT 10) WHERE (strftime('%s', 'now') - last_time_used/1000)
<2592000 at 
android.database.DatabaseUtils.readExceptionFromParcel(DatabaseUtils.java:181) at 
android.database.DatabaseUtils.readExceptionFromParcel(DatabaseUtils.java:137) at 
android.content.ContentProviderProxy.query(ContentProviderNative.java:413) at 
android.content.ContentResolver.query(ContentResolver.java:464) at 
android.content.ContentResolver.query(ContentResolver.java:407) at 
(...)

我的理解是Android内容解析器正在错误地构建SQL查询。我错过了什么吗?有没有人遇到这个或可以建议一个解决方法?是否可以使用其他/更简单的查询?我想为这些Android版本维护此功能:(

0 个答案:

没有答案