tags:

views:

78

answers:

1

Hello all, i am trying to query my database so that it returns only rows with a specific value in a certain column.but when i run it, it still returns all the rows. please take a look at the code and see if i have done it incorrectly. I am trying to query for the string in the PRIORITY column. i want to display those specific rows in a listview. Thank you.

   private static final String DATABASE_NAME = "MemoData.db";
    private static final int DATABASE_VERSION = 1;

    public static final String DATABASE_TABLE = "Places";

    public static final String KEY_ID = "_id";
    public static final String NAME = "Title";
    public static final String CATEGORY = "category";
    public static final String PRIORITY = "priority";

        private static final String TAG = "DBAdapter";

        private static final String DATABASE_CREATE = "create table Places (_id integer primary key autoincrement, Title text, category text, priority text);";


 public Cursor priorityData(){
            String[] resultColumns = new String[] {KEY_ID,NAME,CATEGORY,PRIORITY };
            String[] condition = {"High"};
            Cursor cursor = db.query(DATABASE_TABLE, resultColumns, KEY_ID + "=" + "?",    condition, null, null, null);
              if(cursor.moveToNext()){
                return cursor;
              }
            return cursor;  
        }

The values for priority columns can be "Low", "Medium" or "High". i am trying to query the database to return rows whose values are set as "High" in the PRIORITY column.

A: 

I am trying to query for the string in the PRIORITY column...

So your filter should be

PRIORITY + " = ?"

instead of

KEY_ID + "=" + "?"

Also it's not a good idea to have a text column to hold values from a limited set of possibilities that you are then filtering and your last if makes no sense.

dtmilano
Hey thanks, it works! if you have the time, could you please tell me why its not a good idea to have a text values to hold columns. As i say, am too so experienced with sql; so everything is like a learning for me. Please ignore the if statement, was an error in my part. thanks once again
Rexx