2013-05-05 63 views
2

我得到以下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; 
    } 
+1

逃避你的'name'变量 – 2013-05-05 11:42:41

+0

@SagarWaghmare单引号如果你的意思'” DISPLAY_NAME = \'” +名字+ “\'”'它仍然崩溃例外。 – 2013-05-05 11:46:59

+1

没有。使用'phoneCursor = contentResolver.query(Phone.CONTENT_URI,new String [] {Phone.NUMBER,Phone.TYPE}, “DISPLAY_NAME =?”,new String [] {name},null);' – 2013-05-05 11:50:35

回答

0

理论上,应该做的,

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

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')

出现异常,因为在你的显示名称的单引号的

试试这个

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

An d然后

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

非常感谢您的帮助。 +1 – 2013-05-05 11:58:44

1

它是由于name字符串中的单引号引起的。一种方法是摆脱单引号,但这不是正确的方法。如果您希望代码在单引号和双引号内运行,请尝试使用rawQuery()而不是使用.query()方法。看到上面的2个线程:Android quotes within an sql query stringhandeling querying an item with a single quote in it。他们处理同样的问题。

+0

非常感谢您的帮助。 +1 – 2013-05-05 11:57:35

+0

很高兴为您提供帮助。希望它有助于解决您的问题。 – 2013-05-05 11:59:28