ArloGrant
ArloGrant

Reputation: 249

Cannot load cursor from DB in SimpleCursorAdapter because no such column: _id

I am trying to retrieve a cursor from a database that has a table ( with the _id field) but the simplecursoradapter cannot be created an the error says

  SqlliteException: no such column: _id 

BUT what is wrong in my Database class:

//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 + ")";

And return the cursor

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;
}

This is my activity that shows the 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);

}

The tutorial is from http://developer.android.com/training/search/search.html

Upvotes: 0

Views: 578

Answers (2)

Brent Hronik
Brent Hronik

Reputation: 2427

As a requirement of the CursorAdpater class, the underlying Cursor object must contain a column named "_id", which is why you are getting that error. This is straight from the CursorAdapter Doc: http://developer.android.com/reference/android/widget/CursorAdapter.html

Upvotes: 0

Sam
Sam

Reputation: 86948

You need to use an alias to change FTS' rowid into Android's _id:

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

FTS doesn't let you override the primary key's default column name, since "integer primary key autoincrement" is ignored entirely as "syntactic sugar". In fact, FTS simply interprets your _id column as a generic column...

So you should drop _id from your create-table statement otherwise it adds an extra _id column:

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

Upvotes: 3

Related Questions