自定义搜索建议抛出 SQLiteException
我正在尝试将自定义搜索建议添加到我的搜索对话框(按下搜索按钮时打开)。我按照 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 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论