我的查询有什么问题

时间:2013-11-24 15:47:11

标签: android sqlite

我正在尝试使用CursorLoader

查询数据库

这是来自加载程序的查询

  

11-24 17:33:21.507:E / ACRA(32418):引起:   android.database.sqlite.SQLiteException:near“)”:语法错误(代码   1):,编译时:SELECT _id,contact_id,photo_uri,data1,   查找FROM view_data数据WHERE(1)AND(((mimetype =?OR mimetype =?)   AND data1 like?%))ORDER BY lookup

在“)”附近有什么不对,我看不到? - 这就是我的查询参数看起来像

的原因
Uri AUTO_COMPLETE_CONTACT_URI = Data.CONTENT_URI;

String[] AUTO_COMLETE_PROJECTION = {Data._ID, Data.CONTACT_ID, Data.PHOTO_URI,   Data.DATA1 ,Data.LOOKUP_KEY};
String AUTO_COMPLETE_SELECTION_LIKE_QUERY = AUTO_COMPLETE_SELECTION + " AND " + Data.DATA1 + " like ?";

public static String[] getAutoCompleteQuerySelectionArgs(String query) {
    return new String[] { Email.CONTENT_ITEM_TYPE, Phone.CONTENT_ITEM_TYPE, query+"%" };
}

/** This method is invoked by initLoader() */
@Override
public Loader<Cursor> onCreateLoader(int id, Bundle data) {
    switch(id) {
    case ContactsQuery.AUTO_COMPLETE_QUERY_ID:
        Uri uri = ContactsQuery.AUTO_COMPLETE_CONTACT_URI;  
        if(data!=null) {
        String query = data.getString(ContactsQuery.AUTO_COMPLETE_QUERY);
        return new CursorLoader(getActivity().getBaseContext(), uri, ContactsQuery.AUTO_COMLETE_PROJECTION, ContactsQuery.AUTO_COMPLETE_SELECTION_LIKE_QUERY , Query.getAutoCompleteQuerySelectionArgs(query), ContactsQuery.AUTO_COMPLETE_SORT_ORDER); 
        }else {
            return  new CursorLoader(getActivity().getBaseContext(), uri, ContactsQuery.AUTO_COMLETE_PROJECTION, ContactsQuery.AUTO_COMPLETE_SELECTION , null, ContactsQuery.AUTO_COMPLETE_SORT_ORDER);
        }
    }
return null;
}

/** This method is executed in ui thread, after onCreateLoader() */
@Override
public void onLoadFinished(Loader<Cursor> arg0, Cursor c) { 
    chipsCursorAdapter.swapCursor(c);       
}

@Override
public void onLoaderReset(Loader<Cursor> loader) {
    chipsCursorAdapter.swapCursor(null);
}

1 个答案:

答案 0 :(得分:1)

您的LIKE模式不使用正确的SQL语法。

%必须在字符串内(即引号内),但?必须在外面,所以你必须将它们连接起来:

... AND data1 LIKE ? || '%')) ORDER BY ...