Android: Activity.managedQuery () da SQLdan qochish

Activity.managedQuery() dan foydalangan holda, so'rovni selection argumenti uchun bir qatorda olib boradigan Android ilovasini ishlab chiqyapman. Argument SQL kodini WHERE ga tegishli, ammo WHERE kalit so'zidan tashqari.

Mening ilovam foydalanuvchining manzillar kitobida bo'lishi mumkin bo'lgan shaxslarning familiyasi va familiyalaridan foydalanadi. Biroq, ba'zi bir kishilarda bitta taklif belgisi mavjud bo'lgan ism bor. Masalan, Jon O'Reilly . Buning sababi SQLiteException bitta kodning mag'lubiyatni tugatganligi va Reilly ni qanday ishlashini bilmasligi sabab bo'ladi.

I tried doing a simple:
name = name.replace("'", "\\'");
But this didn't work.

To'liq istisno quyidagicha ko'rinadi:

android.database.sqlite.SQLiteException: near "Reilly": syntax error: , while compiling:     
SELECT raw_contact_id, display_name FROM view_data_restricted data WHERE (1) AND 
(in_visible_group = '1' AND display_name ='John O\'Reilly') ORDER BY display_name 
COLLATE LOCALIZED ASC
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:262)
at android.app.Activity.managedQuery(Activity.java:1550)
at org.jonescb.myApp.MyClass.queryFriends(MyClass.java:68)

Bu mening kodim:

fname = fname.replace("'", "\\'");
Uri contacts = ContactsContract.Data.CONTENT_URI;
String[] projection = new String[] {
    ContactsContract.Data.RAW_CONTACT_ID,
    ContactsContract.Contacts.DISPLAY_NAME
};
String selection = ContactsContract.Contacts.IN_VISIBLE_GROUP +
                " = '1' AND " + ContactsContract.Contacts.DISPLAY_NAME +
                " ='" + fname + "'";
String sortOrder = ContactsContract.Contacts.DISPLAY_NAME +
                " COLLATE LOCALIZED ASC";
Cursor cursor = activity.managedQuery(
                contacts,
                projection,
                selection,
                null,
                sortOrder
                );
0

1 javoblar

selectArguments parametrlaridan foydalaning

Uri contacts = ContactsContract.Data.CONTENT_URI;
String[] projection = new String[] {
    ContactsContract.Data.RAW_CONTACT_ID,
    ContactsContract.Contacts.DISPLAY_NAME
};
String selection = ContactsContract.Contacts.IN_VISIBLE_GROUP +
                " =? AND " + ContactsContract.Contacts.DISPLAY_NAME +
                " =?";
String sortOrder = ContactsContract.Contacts.DISPLAY_NAME +
                " COLLATE LOCALIZED ASC";
Cursor cursor = activity.managedQuery(
                contacts,
                projection,
                selection,
                new String[] {"1", fname},
                sortOrder
                );
3
qo'shib qo'ydi