程序師世界是廣大編程愛好者互助、分享、學習的平台,程序師世界有你更精彩!
首頁
編程語言
C語言|JAVA編程
Python編程
網頁編程
ASP編程|PHP編程
JSP編程
數據庫知識
MYSQL數據庫|SqlServer數據庫
Oracle數據庫|DB2數據庫
 程式師世界 >> 編程語言 >> 更多編程語言 >> 編程綜合問答 >> sqlite-SQLite查詢報出異常

sqlite-SQLite查詢報出異常

編輯:編程綜合問答
SQLite查詢報出異常

在數據庫中執行一個查詢的時候報出異常:

bind or column index out of range

ContentProvider中的查詢如下:

@Override
public Cursor query(Uri uri, String[] projection, String selection, String[] selectionArgs, String sortOrder) {
    SQLiteQueryBuilder queryBuilder = new SQLiteQueryBuilder();
    int uriType = sURIMatcher.match(uri);
    switch (uriType) {
    case RESPONSEVIEW2_SELECTION_10:
        queryBuilder.setTables(RESPONSES_VIEW);
        String activeUserKey = getActiveUserId();;
        String outerSelection = 
                "(((" + PeerCreatedDataColumns.SHARED + "=" + "" + trueId + "" + ") OR " 
                + "(" + PeerCreatedDataColumns.AUTHORHASH + "=" + "'" + activeUserKey + "'" + ")) AND (" 
                + "(" + ResponsesColumns.PRIMARYMATCH + "= '*'" + ") OR " 
                                    // '*'是程序定義的 , 希望不會和別的有沖突
                + "(" + ResponsesColumns.PRIMARYMATCH + " like '?') OR " 
                + "(" + ResponsesColumns.PRIMARYMATCH + " like '" + selectionArgs[0] + "%') OR " 
                + "(" + ResponsesColumns.PRIMARYMATCH + " like '%" + selectionArgs[0] + "') OR " 
                + "(" + ResponsesColumns.PRIMARYMATCH + " like '%" + selectionArgs[0] + "%'" + ")))";
        Log.d(DEBUG_TAG, "query " + outerSelection);
        Cursor cursor10 = queryBuilder.query(mDB.getReadableDatabase(), projection, outerSelection, selectionArgs,
                null, null, sortOrder);
        cursor10.setNotificationUri(getContext().getContentResolver(), uri);
        Log.e(DEBUG_TAG, "responses_view2 should have better search function");
        return cursor10;
    case ROW_SELECTION_11:
        queryBuilder.setTables(RESPONSES_VIEW);
        Log.e(DEBUG_TAG, "responses_view2 should have better search function");
        queryBuilder.appendWhere(PeerDatabase._ID + "=" + uri.getLastPathSegment());
        Cursor cursor11 = queryBuilder.query(mDB.getReadableDatabase(), projection, selection, selectionArgs, null,
                null, sortOrder);
        cursor11.setNotificationUri(getContext().getContentResolver(), uri);
        return cursor11;
    ...
    }
}

然後cursor loader如下:

public Loader<Cursor> onCreateLoader(int id, Bundle args) {
    if (search == null) {
        return null;
    } else {
        String[] selectArgs = { search };
        return new CursorLoader(getActivity(), table, createProjection, "?", selectArgs, null);
    }
}

報出錯誤如下:

12-09 18:25:54.509: E/AndroidRuntime(12461): FATAL EXCEPTION: ModernAsyncTask #2
12-09 18:25:54.509: E/AndroidRuntime(12461): java.lang.RuntimeException: An error occured while executing doInBackground()
12-09 18:25:54.509: E/AndroidRuntime(12461):    at android.support.v4.content.ModernAsyncTask$3.done(ModernAsyncTask.java:137)
12-09 18:25:54.509: E/AndroidRuntime(12461):    at java.util.concurrent.FutureTask$Sync.innerSetException(FutureTask.java:274)
12-09 18:25:54.509: E/AndroidRuntime(12461):    at java.util.concurrent.FutureTask.setException(FutureTask.java:125)
12-09 18:25:54.509: E/AndroidRuntime(12461):    at java.util.concurrent.FutureTask$Sync.innerRun(FutureTask.java:308)
12-09 18:25:54.509: E/AndroidRuntime(12461):    at java.util.concurrent.FutureTask.run(FutureTask.java:138)
12-09 18:25:54.509: E/AndroidRuntime(12461):    at java.util.concurrent.ThreadPoolExecutor.runWorker(ThreadPoolExecutor.java:1088)
12-09 18:25:54.509: E/AndroidRuntime(12461):    at java.util.concurrent.ThreadPoolExecutor$Worker.run(ThreadPoolExecutor.java:581)
12-09 18:25:54.509: E/AndroidRuntime(12461):    at java.lang.Thread.run(Thread.java:1019)
12-09 18:25:54.509: E/AndroidRuntime(12461): Caused by: android.database.sqlite.SQLiteException: bind or column index out of range: handle 0x28bd08
12-09 18:25:54.509: E/AndroidRuntime(12461):    at android.database.sqlite.SQLiteProgram.native_bind_string(Native Method)
12-09 18:25:54.509: E/AndroidRuntime(12461):    at android.database.sqlite.SQLiteProgram.bindString(SQLiteProgram.java:244)
12-09 18:25:54.509: E/AndroidRuntime(12461):    at android.database.sqlite.SQLiteQuery.bindString(SQLiteQuery.java:185)
12-09 18:25:54.509: E/AndroidRuntime(12461):    at android.database.sqlite.SQLiteDirectCursorDriver.query(SQLiteDirectCursorDriver.java:48)
12-09 18:25:54.509: E/AndroidRuntime(12461):    at android.database.sqlite.SQLiteDatabase.rawQueryWithFactory(SQLiteDatabase.java:1356)
12-09 18:25:54.509: E/AndroidRuntime(12461):    at android.database.sqlite.SQLiteQueryBuilder.query(SQLiteQueryBuilder.java:330)
12-09 18:25:54.509: E/AndroidRuntime(12461):    at android.database.sqlite.SQLiteQueryBuilder.query(SQLiteQueryBuilder.java:280)
12-09 18:25:54.509: E/AndroidRuntime(12461):    at org.anuvar.plana.m.provider.PeerDataProvider.query(PeerDataProvider.java:285)
12-09 18:25:54.509: E/AndroidRuntime(12461):    at android.content.ContentProvider$Transport.query(ContentProvider.java:187)
12-09 18:25:54.509: E/AndroidRuntime(12461):    at android.content.ContentResolver.query(ContentResolver.java:262)
12-09 18:25:54.509: E/AndroidRuntime(12461):    at android.support.v4.content.CursorLoader.loadInBackground(CursorLoader.java:49)
12-09 18:25:54.509: E/AndroidRuntime(12461):    at android.support.v4.content.CursorLoader.loadInBackground(CursorLoader.java:35)
12-09 18:25:54.509: E/AndroidRuntime(12461):    at android.support.v4.content.AsyncTaskLoader.onLoadInBackground(AsyncTaskLoader.java:240)
12-09 18:25:54.509: E/AndroidRuntime(12461):    at android.support.v4.content.AsyncTaskLoader$LoadTask.doInBackground(AsyncTaskLoader.java:51)
12-09 18:25:54.509: E/AndroidRuntime(12461):    at android.support.v4.content.AsyncTaskLoader$LoadTask.doInBackground(AsyncTaskLoader.java:40)
12-09 18:25:54.509: E/AndroidRuntime(12461):    at android.support.v4.content.ModernAsyncTask$2.call(ModernAsyncTask.java:123)
12-09 18:25:54.509: E/AndroidRuntime(12461):    at java.util.concurrent.FutureTask$Sync.innerRun(FutureTask.java:306)
12-09 18:25:54.509: E/AndroidRuntime(12461):    ... 4 more

請幫忙指點一下錯誤的原因,或者還有更好的查詢方法,謝謝。

最佳回答:


不要把?包在括號裡:

" like '?') OR "

換成

" like ?) OR "

如果搜索是用戶輸入, 還要防止SQL注入攻擊:

+ "(" + ResponsesColumns.PRIMARYMATCH + " like ?) OR " 
+ "(" + ResponsesColumns.PRIMARYMATCH + " like ?) OR " 
+ "(" + ResponsesColumns.PRIMARYMATCH + " like ?) OR " 
+ "(" + ResponsesColumns.PRIMARYMATCH + " like ?)))";

創建一個新的selectionArgs參數:

String search = selectionArgs[0];
String[] matchArgs = { search, search + "%", "%" + search, "%" + search + "%" };
Cursor cursor10 = queryBuilder.query(mDB.getReadableDatabase(), projection, 
            outerSelection, matchArgs, null, null, sortOrder);
  1. 上一頁:
  2. 下一頁:
Copyright © 程式師世界 All Rights Reserved