0

在我的 ContentProvider 中导致错误的方法

public static Cursor getSuggestions(String query) {
    SQLiteDatabase db = dbHelper.getReadableDatabase();
    db.beginTransaction();
    try {
        String selection = Formula.FORMULA_NAME + " LIKE %?%";
        String[] selectionArgs = { query + "*" };
        Cursor cursor = dbHelper.getReadableDatabase().query(
                FORMULA_TABLE_NAME,
                new String[] { BaseColumns._ID,
                        SearchManager.SUGGEST_COLUMN_TEXT_1, 
                        BaseColumns._ID + " AS " + SearchManager.SUGGEST_COLUMN_INTENT_DATA_ID 
                        }, 
                        selection,
                selectionArgs, null, null, null);
        db.setTransactionSuccessful();
        return cursor;
    } catch (SQLiteException e) {
    } finally {
        db.endTransaction();
    }
    throw new SQLException("Failed to begin transaction");
}

数据库创建:

db.execSQL("CREATE TABLE " + FORMULA_TABLE_NAME + " (" +
                BaseColumns._ID + " INTEGER PRIMARY KEY AUTOINCREMENT," +
                SearchManager.SUGGEST_COLUMN_TEXT_1 + " TEXT," +
                Formula.CATEGORY + " TEXT" +
                ");");

使用的常量:

    public static final String FORMULA_NAME = SearchManager.SUGGEST_COLUMN_TEXT_1;
    public static final String CATEGORY = "category";

问题是,在我的方法中,事务不成功,因为它引发了错误:throw new SQLException("Failed to begin transaction");我正在尝试做的是搜索数据库作为搜索的一部分。当用户激活搜索框时,我对其进行了设置,以便此方法应根据其名称返回带有可疑项目的光标。通过调试,我推断问题出在我的 Content Provider 内部的搜索方法上。有什么解决方案或想法吗?

4

1 回答 1

1

我猜这条线throw new SQLException("Failed to begin transaction");应该在你的 catch 块内。

如果我们将所有内容简化为:

public static Cursor getSuggestions(String query) {
    SQLiteDatabase db = dbHelper.getReadableDatabase();
    String selection = Formula.FORMULA_NAME + " LIKE %?%";
    String[] selectionArgs = { query + "*" };
    Cursor cursor = db.query(
            FORMULA_TABLE_NAME,
            new String[] { BaseColumns._ID,
                    SearchManager.SUGGEST_COLUMN_TEXT_1, 
                    BaseColumns._ID + " AS " + SearchManager.SUGGEST_COLUMN_INTENT_DATA_ID 
                    }, 
            selection,
            selectionArgs, null, null, null);
    return cursor;
} 
于 2012-04-14T18:32:30.807 回答