0

我正在为安卓开发。我正在使用参考http://sqlite.org/fts3.html。我创建了一个 fts 表作为

final String SQL_CREATE_TABLE_SEARCHABLE =
    
        new StringBuilder("CREATE VIRTUAL TABLE ")
    
                .append(SearchableEntry.TABLE_NAME)
    
                .append(" USING fts4(")

                    .append(SearchableEntry.COLUMN_DOG_ID).append(", ")

                    .append(SearchableEntry.COLUMN_BREED).append(", ")
    
                .append(SearchableEntry.COLUMN_CATEGORY).append(",")

                     .append(SearchableEntry.COLUMN_DOG_NAME).append(",")

                    .append(SearchableEntry.COLUMN_DOCUMENTATION)
    
                .append(");").toString();

    sqLiteDatabase.execSQL(SQL_CREATE_TABLE_SEARCHABLE);

COLUMN_DOG_ID 实际上是文本。它是一个字符串,而不是一个数字。在任何情况下,我都会创建表,然后向其中添加数据;似乎没有任何麻烦。但是当我尝试查询时,我总是遇到死胡同:我得到了异常。这是查询

private Cursor getSearchableByTokens(Uri uri, String[] projection) {

        DogLog.d(TAG,"getSearchableByTokens with uri %s",uri);

        String table = DogContractt.SearchableEntry.TABLE_NAME;

        String selection = table + " MATCH ? ";

        String queryText = DogContractt.SearchableEntry.getQueryTextFromUri(uri);

        String[] selectionArgs = {queryText};

        if (null == mOpenHelper) {
    
        mOpenHelper = new DogsDBHelper(getContext());

        }

        SQLiteDatabase readableDB = mOpenHelper.getReadableDatabase();

        Cursor cursor = readableDB.query(table, projection, selection, selectionArgs, null, null, null);

        if (null != cursor) {
            cursor.setNotificationUri(getContext().getContentResolver(), DogContractt.SearchableEntry.CONTENT_URI);

        }
    
    return cursor;
    
}

错误

FATAL EXCEPTION: main
Process: com.company.android, PID: 18696
java.lang.RuntimeException: Unable to start activity ComponentInfo{com.company.android/com.company.android.activities.SearchableActivity}: java.lang.IllegalArgumentException: column '_id' does not exist
at android.app.ActivityThread.performLaunchActivity(ActivityThread.java:3122)
at android.app.ActivityThread.handleLaunchActivity(ActivityThread.java:3221)
at android.app.ActivityThread.access$1000(ActivityThread.java:198)
at android.app.ActivityThread$H.handleMessage(ActivityThread.java:1676)
at android.os.Handler.dispatchMessage(Handler.java:102)
at android.os.Looper.loop(Looper.java:145)
at android.app.ActivityThread.main(ActivityThread.java:6840)
at java.lang.reflect.Method.invoke(Native Method)
at java.lang.reflect.Method.invoke(Method.java:372)
at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:1404)
at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:1199)
 Caused by: java.lang.IllegalArgumentException: column '_id' does not exist
at android.database.AbstractCursor.getColumnIndexOrThrow(AbstractCursor.java:303)
at android.database.CursorWrapper.getColumnIndexOrThrow(CursorWrapper.java:78)
at android.support.v4.widget.CursorAdapter.init(CursorAdapter.java:174)
at android.support.v4.widget.CursorAdapter.<init>(CursorAdapter.java:138)
at com.company.android.activities.SearchableActivity$2.<init>(SearchableActivity.java:0)
at com.company.android.activities.SearchableActivity.searchableResultsList(SearchableActivity.java:81)
at com.company.android.activities.SearchableActivity.searchContentProvider(SearchableActivity.java:73)
at com.company.android.activities.SearchableActivity.onCreate(SearchableActivity.java:59)
at android.app.Activity.performCreate(Activity.java:6535)
at android.app.Instrumentation.callActivityOnCreate(Instrumentation.java:1120)
at android.app.ActivityThread.performLaunchActivity(ActivityThread.java:3075)
at android.app.ActivityThread.handleLaunchActivity(ActivityThread.java:3221) 
at android.app.ActivityThread.access$1000(ActivityThread.java:198) 
at android.app.ActivityThread$H.handleMessage(ActivityThread.java:1676) 
at android.os.Handler.dispatchMessage(Handler.java:102) 
at android.os.Looper.loop(Looper.java:145) 
at android.app.ActivityThread.main(ActivityThread.java:6840) 
at java.lang.reflect.Method.invoke(Native Method) 
at java.lang.reflect.Method.invoke(Method.java:372) 
at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:1404) 
at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:1199) 
4

1 回答 1

0

游标适配器需要一个名为 的唯一数字列_id。这适用于每个查询结果,无论是来自 FTS 表还是普通表。

FTS 表有一个内部docid;只需将其添加到投影中:

String[] projection = new String[] { "docid AS _id", ... };
于 2016-05-12T07:58:52.187 回答