如何将特定字段与SQLite中的字符串进行比较

时间:2016-09-14 17:36:33

标签: android sqlite android-sqlite

Cons.getpersonIdForRegisteredUser()方法返回一个字符串 我的查询分为两部分:一个是检查人id,另一个是data enabled

@Override
public Loader<Cursor> onCreateLoader(final int i, final Bundle bundle) {

    String sortOrder = ContactProviderContract.ContactDatabaseEntry.COLUMN_NAME_FIRST_NAME
                + " COLLATE NOCASE ASC";

    String selection = ContactProviderContract.
                ContactDatabaseEntry.COLUMN_NAME_PERSON_ID + " != " + Cons.getPersonIdForRegisteredUser(getActivity().getApplicationContext())
                + " AND "
                + ContactProviderContract.ContactDatabaseEntry.COLUMN_NAME_DATA_ENABLED + " = ?";
    String[] selectionArgs = { "1" };

    return new CursorLoader(getActivity().getApplicationContext(), 
                   ContactProviderContract.CONTACTS_URI, 
                   ContactProviderContract.ContactDatabaseEntry.MIN_DETAILS_COLUMNS,
                   selection, 
                   selectionArgs, 
                   sortOrder);
}

错误是从方法Cons.getpersonid...()检索到的字符串被视为一列而不是要比较的字符串。

1 个答案:

答案 0 :(得分:0)

如果我理解错误,那么您应该将Cons.getPersonId...放入selectionArgs

String selection = ContactProviderContract.
            ContactDatabaseEntry.COLUMN_NAME_PERSON_ID + " != ?" +
            + " AND "
            + ContactProviderContract.ContactDatabaseEntry.COLUMN_NAME_DATA_ENABLED + " = ?";
String[] selectionArgs = { "1" , Cons.getPersonIdForRegisteredUser(getActivity().getApplicationContext()) };