【问题标题】:Cannot load cursor from DB in SimpleCursorAdapter because no such column: _id无法从 SimpleCursorAdapter 中的 DB 加载游标,因为没有这样的列:_id
【发布时间】:2013-02-22 21:43:35
【问题描述】:

我正在尝试从具有表(带有 _id 字段)的数据库中检索游标,但是 无法创建简单的光标适配器,错误提示

  SqlliteException: no such column: _id 

但是我的数据库类有什么问题:

//The columns we'll include in the dictionary table
public static final String COLUMN_ID = "_id";
public static final String COL_WORD = "WORD";
public static final String COL_DEFINITION = "DEFINITION";


private static final String FTS_TABLE_CREATE =
                "CREATE VIRTUAL TABLE " + FTS_VIRTUAL_TABLE +
                " USING fts3 (" + COLUMN_ID + " integer primary key autoincrement, " +
                COL_WORD + ", " +
                COL_DEFINITION + ")";

并返回光标

public Cursor getWordMatches(String query, String[] columns) {
    String selection = COL_WORD + " MATCH ?";
    String[] selectionArgs = new String[] {query+"*"};

    return query(selection, selectionArgs, columns);
}

private Cursor query(String selection, String[] selectionArgs, String[] columns) {
    SQLiteQueryBuilder builder = new SQLiteQueryBuilder();
    builder.setTables(FTS_VIRTUAL_TABLE);

    String[] col = new String[] {COLUMN_ID, COL_WORD};
    //ubacio null - col
    Cursor cursor = builder.query(mDatabaseOpenHelper.getReadableDatabase(),
            col, selection, selectionArgs, null, null, null);

    if (cursor == null) {
        return null;
    } else if (!cursor.moveToFirst()) {
        cursor.close();
        return null;
    }
    return cursor;
}

这是我显示 ListView 的活动

          //query the db class method getWordMatches()
      Cursor c = db.getWordMatches(query, null);
          displayWords(c);

      }
    }


public void displayWords(Cursor c){

    // Creates a new SimpleCursorAdapter
    SimpleCursorAdapter mCursorAdapter = new SimpleCursorAdapter(
        getApplicationContext(),                    // The application's Context object
        android.R.layout.simple_list_item_1,        // A layout in XML for one row in the ListView
        c,                                          // The result from the query
        new String[] {DatabaseTable.COL_WORD},      // A string array of column names in the cursor
        new int[] { android.R.id.text1 });          // An integer array of view IDs in the row layout


    // Sets the adapter for the ListView
    setListAdapter(mCursorAdapter);

}

教程来自http://developer.android.com/training/search/search.html

【问题讨论】:

    标签: android simplecursoradapter


    【解决方案1】:

    您需要使用别名将 FTS 的 rowid 更改为 Android 的 _id

    String[] col = new String[] {"rowid as " + COLUMN_ID, COL_WORD};
    

    FTS 不允许您覆盖主键的默认列名,因为 "integer primary key autoincrement" 被完全忽略为 "syntactic sugar"。事实上,FTS 只是将您的 _id 列解释为通用列...

    所以你应该从你的 create-table 语句中删除_id,否则它会添加一个额外的_id 列:

    private static final String FTS_TABLE_CREATE =
                "CREATE VIRTUAL TABLE " + FTS_VIRTUAL_TABLE +
                " USING fts3 (" + COL_WORD + ", " + COL_DEFINITION + ")";
    

    【讨论】:

      【解决方案2】:

      作为 CursorAdpater 类的要求,底层 Cursor 对象必须包含名为“_id”的列,这就是您收到该错误的原因。这直接来自 CursorAdapter 文档:http://developer.android.com/reference/android/widget/CursorAdapter.html

      【讨论】:

        猜你喜欢
        • 1970-01-01
        • 1970-01-01
        • 2015-09-08
        • 2016-02-07
        • 2015-07-06
        • 2020-02-13
        • 1970-01-01
        • 1970-01-01
        • 2016-05-17
        相关资源
        最近更新 更多