自定义搜索建议抛出 SQLiteException

发布于 2024-11-03 10:51:20 字数 4122 浏览 0 评论 0原文

我正在尝试将自定义搜索建议添加到我的搜索对话框(按下搜索按钮时打开)。我按照 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

I am trying to add custom search suggestions to my search dialog (opened when search button is pressed). I followed parts of the guide at the docs. Everything works except that I get:

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)

Here is the relevant code in my 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");
}

What could be the problem?
UPDATE:
The query changes depending on the characters I type:

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

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。
列表为空,暂无数据
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文