我得到以下sqlite的例外,同时查询联系电话:使用光标获取联系人抛出异常的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;
}
逃避你的'name'变量 – 2013-05-05 11:42:41
@SagarWaghmare单引号如果你的意思'” DISPLAY_NAME = \'” +名字+ “\'”'它仍然崩溃例外。 – 2013-05-05 11:46:59
没有。使用'phoneCursor = contentResolver.query(Phone.CONTENT_URI,new String [] {Phone.NUMBER,Phone.TYPE}, “DISPLAY_NAME =?”,new String [] {name},null);' – 2013-05-05 11:50:35