自定义搜索建议会抛出SQLiteException

时间:2011-04-26 00:24:56

标签: java android sqlite search

我正在尝试将自定义搜索建议添加到我的搜索对话框(按下搜索按钮时打开)。我在docs处遵循了指南的部分内容。除了我得到的一切都有效:

04-25 20:18:05.638: INFO/Database(19418): sqlite returned: error code = 1, msg = prepared statement aborts at 7: [SELECT _id, suggest_text_1 FROM formula WHERE suggest_text_1 MATCH ?]
04-25 20:18:05.664: WARN/SuggestionsAdapter(19418): Search suggestions query threw an exception.
04-25 20:18:05.664: WARN/SuggestionsAdapter(19418): android.database.sqlite.SQLiteException: SQL logic error or missing database
04-25 20:18:05.664: WARN/SuggestionsAdapter(19418):     at android.database.sqlite.SQLiteQuery.native_fill_window(Native Method)
04-25 20:18:05.664: WARN/SuggestionsAdapter(19418):     at android.database.sqlite.SQLiteQuery.fillWindow(SQLiteQuery.java:70)
04-25 20:18:05.664: WARN/SuggestionsAdapter(19418):     at   android.database.sqlite.SQLiteCursor.fillWindow(SQLiteCursor.java:293)
04-25 20:18:05.664: WARN/SuggestionsAdapter(19418):     at android.database.sqlite.SQLiteCursor.getCount(SQLiteCursor.java:266)
04-25 20:18:05.664: WARN/SuggestionsAdapter(19418):     at android.content.ContentResolver.query(ContentResolver.java:251)
04-25 20:18:05.664: WARN/SuggestionsAdapter(19418):     at android.app.SearchManager.getSuggestions(SearchManager.java:760)
04-25 20:18:05.664: WARN/SuggestionsAdapter(19418):     at android.app.SuggestionsAdapter.runQueryOnBackgroundThread(SuggestionsAdapter.java:166)
04-25 20:18:05.664: WARN/SuggestionsAdapter(19418):     at android.widget.CursorFilter.performFiltering(CursorFilter.java:49)
04-25 20:18:05.664: WARN/SuggestionsAdapter(19418):     at android.widget.Filter$RequestHandler.handleMessage(Filter.java:234)
04-25 20:18:05.664: WARN/SuggestionsAdapter(19418):     at android.os.Handler.dispatchMessage(Handler.java:99)
04-25 20:18:05.664: WARN/SuggestionsAdapter(19418):     at android.os.Looper.loop(Looper.java:123)
04-25 20:18:05.664: WARN/SuggestionsAdapter(19418):     at android.os.HandlerThread.run(HandlerThread.java:60)

以下是我ContentProvider中的相关代码:

@Override
public Cursor query(Uri uri, String[] projection, String selection,
        String[] selectionArgs, String sortOrder) {
    SQLiteQueryBuilder qb = new SQLiteQueryBuilder();

    switch(uriMatcher.match(uri)) {
    case FORMULAS:
        qb.setTables(FORMULA_TABLE_NAME);
        qb.setProjectionMap(projectionMap);
        break;
    case FORMULA_ID:
        qb.setTables(FORMULA_TABLE_NAME);
        qb.setProjectionMap(projectionMap);
        qb.appendWhere(Formula._ID + "=" + uri.getPathSegments().get(1));
        break;
    case SEARCH_SUGGEST:
        if (selectionArgs == null) {
            throw new IllegalArgumentException("selectionArgs cannot be null!");
        }
        return getSuggestions(selectionArgs[0]);
    default:
        throw new IllegalArgumentException("Unknown URI: " + uri);
    }

    SQLiteDatabase db = dbHelper.getWritableDatabase();
    Cursor cursor = qb.query(db, projection, selection, selectionArgs, null, null, sortOrder);

    cursor.setNotificationUri(getContext().getContentResolver(), uri);
    return cursor;
}

private Cursor getSuggestions(String query) {
    SQLiteDatabase db = dbHelper.getReadableDatabase();
    db.beginTransaction();
    try {
        String selection = Formula.FORMULA_NAME + " MATCH ?";
        String[] selectionArgs = { query + "*" };
        Cursor cursor = dbHelper.getReadableDatabase().query(
                FORMULA_TABLE_NAME,
                new String[] { BaseColumns._ID,
                        SearchManager.SUGGEST_COLUMN_TEXT_1 }, selection,
                selectionArgs, null, null, null);
        db.setTransactionSuccessful();
        return cursor;
    } catch (Exception e) {

    } finally {
        db.endTransaction();
    }
    throw new SQLException("Failed to begin transaction");
}

可能是什么问题?
更新:
查询会根据我输入的字符而改变:

04-25 20:49:06.514: ERROR/[Provider]===========>(19715): Q

0 个答案:

没有答案