Warning: file_get_contents(/data/phpspider/zhask/data//catemap/3/android/182.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
Android 使用游标获取联系人会引发sqlite异常_Android_Android Sqlite_Android Contacts_Android Cursor - Fatal编程技术网

Android 使用游标获取联系人会引发sqlite异常

Android 使用游标获取联系人会引发sqlite异常,android,android-sqlite,android-contacts,android-cursor,Android,Android Sqlite,Android Contacts,Android Cursor,我在查询联系人电话时遇到以下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.it

我在查询联系人电话时遇到以下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;
  }
public ArrayList getContacts(){
ArrayList数据=新的ArrayList();
contactCursor=contentResolver.query(Contacts.CONTENT\u URI,新字符串[]{Contacts.DISPLAY\u NAME,BaseColumns.\u ID},null,null,null);
如果(contactCursor!=null){
字符串名称=null;
字符串phoneString=null;
长电话号码=-1;
长id;
InputStream image=null;
while(contactCursor.moveToNext()){
name=contactCursor.getString(contactCursor.getColumnIndex(Contacts.DISPLAY_name));
image=Contacts.openContactPhotoInputStream(contentResolver,ContentUris.withAppendedId(Contacts.CONTENT\u URI,
getLong(contactCursor.getColumnIndex(BaseColumns.\u ID));
id=contactCursor.getLong(contactCursor.getColumnIndex(BaseColumns.\u id));
phoneCursor=contentResolver.query(Phone.CONTENT_URI,新字符串[]{Phone.NUMBER,Phone.TYPE},
“显示名称='”+NAME+“'”,空,空);
while(phoneCursor.moveToNext()){
...
}
}
}
返回数据;
}
android.database.sqlite.SQLiteException:靠近“s”:语法错误:,编译时: 从视图中选择数据1、数据2,其中(1和mimetype='vnd.android.cursor.item/phone_v2')和(DISPLAY_NAME='Seller's Permit Rose Agent')

出现异常,因为显示名称中有一个引号

试试这个

name=name.replace("'","''");
然后

 phoneCursor = contentResolver.query(Phone.CONTENT_URI, new String[] { Phone.NUMBER, Phone.TYPE },
            " DISPLAY_NAME = '" + name + "'", null, null);

这是由于
名称
字符串中的单引号引起的。一种方法是摆脱单一报价,但这不是正确的方法。如果希望代码即使使用单引号和双引号也能工作,请尝试使用rawQuery()而不是.query()方法。请参见以上两个线程:和。它们处理相同的问题。

理想情况下,您应该这样做

phoneCursor = contentResolver.query(Phone.CONTENT_URI, new String[] { Phone.NUMBER, Phone.TYPE }, " DISPLAY_NAME = ?", new String[] {name}, null); 

名称中的单引号转义variable@SagarWaghmare如果您的意思是
“DISPLAY\u NAME=\'”+NAME+“\'”
,它仍然会崩溃,出现异常。否。使用
phoneCursor=contentResolver.query(Phone.CONTENT\u URI,新字符串[]{Phone.NUMBER,Phone.TYPE},“DISPLAY\u NAME=?”,新字符串[]{NAME},null)@SagarWaghmare您是否介意重新发布作为回复,这样我就可以接受您的回答:因为您是第一个回复的人。我已经重新发布了。:)非常感谢你的帮助+请帮忙。希望这有助于解决您的问题。非常感谢您的帮助+1.