0

我的应用程序有问题。一些代码,这会产生错误:

        String selection = ContactsContract.CommonDataKinds.Phone.DISPLAY_NAME
            + " like'%" + name + "%'";
    String[] projection = new String[] { ContactsContract.CommonDataKinds.Phone.NUMBER };
    Cursor c = context.getContentResolver().query(
            ContactsContract.CommonDataKinds.Phone.CONTENT_URI, projection,
            selection, null, null);

这是我的错误:

java.lang.RuntimeException: Failure delivering result ResultInfo{who=null, request=4321, result=-1, data=Intent { (has extras) }} to activity ... android.database.sqlite.SQLiteException: near "m": syntax error: , while compiling: SELECT data1 FROM view_data_restricted data WHERE (1 AND mimetype = 'vnd.android.cursor.item/phone_v2') AND (display_name like'%I'm in a pool%') Caused by: android.database.sqlite.SQLiteException: near "m": syntax error: , while compiling: SELECT data1 FROM view_data_restricted data WHERE (1 AND mimetype = 'vnd.android.cursor.item/phone_v2') AND (display_name like'%I'm in a pool%') at android.database.DatabaseUtils.readExceptionFromParcel(DatabaseUtils.java:158) at android.database.DatabaseUtils.readExceptionFromParcel(DatabaseUtils.java:114) at android.content.ContentProviderProxy.bulkQueryInternal(ContentProviderNative.java:330) at android.content.ContentProviderProxy.query(ContentProviderNative.java:366) at android.content.ContentResolver.query(ContentResolver.java:245)

Aneone 知道什么是错的吗?

4

1 回答 1

0

AND (display_name like'%I'm in a pool%')

您在I'm. 这就是为什么你的错误抱怨SQLiteException: near "m": syntax error

您需要在插入引号之前对其进行转义。

于 2012-10-09T21:01:18.160 回答