Search code examples
androidandroid-sqlite

RawQuery data cursor showing empty field


I try to pull data from database by using RawQuery method, but it seems I get empty rows. Actually data is present in the database which I am try to pull. Can anyone find my mistake?

db = dbhelper.getReadableDatabase();

String selectSQL = "Select * from " + Wish_list_Table.TABLE_NAME
                + " where " + Wish_list_Table.COL_CATEGORY + " = ?";

String[] add = { " HOME" };

TextView textView = getGenericView();
Cursor selectdata = db.rawQuery(selectSQL, add);
Log.i("select", "" + selectdata.getCount());

This class for database table property......

public static abstract class Wish_list_Table implements BaseColumns {
    public static final String TABLE_NAME = "WISH_LIST1";
    public static final String COL_NAME = "NAME";
    public static final String COL_TIME = "TIME";

    public static final String COL_DATE = "DATE";
    public static final String COL_CATEGORY = "CATEGORY";

    public static final String COL_DESC = "DESC";

}
        

this is DATABASE IMAGE...

enter image description here


Solution

  • Look at this:

    String[] add = { " HOME" };
    

    Don't you see there's a SPACE before "HOME"? It will never match the values in the db!

    So you'll always get an empty Cursor.
    Replace that statement with this:

    String[] add = { "HOME" };
    

    And you'll get all the records where CATEGORY equals 'HOME'