Search code examples
androidsqlitecustom-cursor

Android SQLite column value does not exists issue


need help in some wired problem. I have a sqlite table items as following

item_id    item_name
1          first
2          second
3          third

and so on

this piece of code is inserting item_id and item_name to arrays perfectly fine. But when i assign it to CustomCursorAdapter it through exception

"column 'first' does not exists"

please help I am newbie to Android.

        c = db.rawQuery("select item_id as _id, item_name from items", null);
        int i = 0;
        c.moveToFirst();
        do {
            from[i] = c.getString(1);
            to[i] = c.getInt(0);
            Log.i("item ", to[i] + " " + from[i]);
            i++;
        } while (c.moveToNext());
        try {
            CustomCursorAdapter ca = new CustomCursorAdapter(this,
                    android.R.layout.simple_list_item_1, c, from, to);

            getListView().setAdapter(ca);
        } catch (Exception e) {
            Log.e("Ex ", e.getMessage()); // exception : column 'first' does not exists.
        }

Solution

  • This is because you are setting from[0] = c.getString(1).

    getString() is zero-based so in your case is returning "first". Then you are passing from as the from parameter of a class (that I guess, extends SimpleCursorAdapter) and then you are receiving this error when the adapter is trying to map this column name. In this case from represents the list of column names holding the data to bind to the UI, which is represented by to.