查询联系电话时出现以下 sqlite 异常:
05-05 04:25:47.276: E/AndroidRuntime(7369): android.database.sqlite.SQLiteException: near "s": syntax error: , while compiling:
SELECT data1, data2 FROM view_data_restricted data WHERE (1 AND mimetype = 'vnd.android.cursor.item/phone_v2') AND ( DISPLAY_NAME = 'Seller's Permit Rose Agent')
05-05 04:25:47.276: E/AndroidRuntime(7369): at android.database.DatabaseUtils.readExceptionFromParcel(DatabaseUtils.java:158)
05-05 04:25:47.276: E/AndroidRuntime(7369): at android.database.DatabaseUtils.readExceptionFromParcel(DatabaseUtils.java:114)
05-05 04:25:47.276: E/AndroidRuntime(7369): at android.content.ContentProviderProxy.bulkQueryInternal(ContentProviderNative.java:330)
05-05 04:25:47.276: E/AndroidRuntime(7369): at android.content.ContentProviderProxy.query(ContentProviderNative.java:366)
05-05 04:25:47.276: E/AndroidRuntime(7369): at android.content.ContentResolver.query(ContentResolver.java:245)
我对 sqlite 一无所知,因为我什至没有直接使用它。我正在使用光标。异常的根源是以下行:
phoneCursor = contentResolver.query(Phone.CONTENT_URI, new String[] { Phone.NUMBER, Phone.TYPE },
" DISPLAY_NAME = '" + name + "'", null, null);
完整的方法是
public ArrayList<Contact> getContacts() {
ArrayList<Contact> data = new ArrayList<Contact>();
contactCursor = contentResolver.query(Contacts.CONTENT_URI, new String[] { Contacts.DISPLAY_NAME, BaseColumns._ID }, null, null, null);
if (contactCursor != null) {
String name = null;
String phoneString = null;
long phoneNumber = -1;
long id;
InputStream image = null;
while (contactCursor.moveToNext()) {
name = contactCursor.getString(contactCursor.getColumnIndex(Contacts.DISPLAY_NAME));
image = Contacts.openContactPhotoInputStream(contentResolver, ContentUris.withAppendedId(Contacts.CONTENT_URI,
contactCursor.getLong(contactCursor.getColumnIndex(BaseColumns._ID))));
id = contactCursor.getLong(contactCursor.getColumnIndex(BaseColumns._ID));
phoneCursor = contentResolver.query(Phone.CONTENT_URI, new String[] { Phone.NUMBER, Phone.TYPE },
" DISPLAY_NAME = '" + name + "'", null, null);
while (phoneCursor.moveToNext()) {
...
}
}
}
return data;
}