Pouton Gerald
Pouton Gerald

Reputation: 1625

using cursor to get contacts throw sqlite exception

I get the following sqlite exception while querying for contact phone:

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)

I don't know anything about sqlite, as I am not even using it directly. I am using a cursor. The root of the exception is the line:

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

The complete method is

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;
  }

Upvotes: 2

Views: 782

Answers (3)

Sagar Waghmare
Sagar Waghmare

Reputation: 4742

You should ideally do,

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

Upvotes: 0

Shobhit Puri
Shobhit Puri

Reputation: 26007

Its due to the single quote in the name String. one way is to get rid of the single quote but that's not the right way. If you want that the code should work even with single and double quotes in it, try using rawQuery() instead of using .query() method. See the above 2 threads: Android quotes within an sql query string and handeling querying an item with a single quote in it . They deal with the same issue.

Upvotes: 1

Pragnani
Pragnani

Reputation: 20155

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

Exception occured because of the single quote in your display name

Try this

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

And then

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

Upvotes: 2

Related Questions