0

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.

4
  • You have not provided us with the database schema or the values of most of the constants in your code snippet. Commented Oct 11, 2010 at 17:55
  • Am sorry, i am not sure what you really mean. but the constants are columns for the database table. i have edited my question above in case thats what you meant. but if you require something different, please let me know. thanks once again Commented Oct 11, 2010 at 19:50
  • 1
    I haven't worked with db in android yet, but do you need a space after and before the '=' symbol: KEY_ID + " = ?" Commented Oct 11, 2010 at 21:26
  • i really don't know, i don't have too much experience with sqlite db, but i should guess its the same. But if you have an experience working with sqlite db.. could you please tell me if the code is correct or what am doing wrong. Commented Oct 11, 2010 at 22:41

2 Answers 2

1

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.

Sign up to request clarification or add additional context in comments.

1 Comment

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
1

I think he meant that you should use ENUM type to your PRIORITY variable, because it has limited set of possibilities.

So it should looks something like this:

priority ENUM('LOW','MEDIUM','HIGH') NOT NULL DEFAULT 'MEDIUM'

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.