moDev
moDev

Reputation: 5258

How to use query for multiple selection argument using one selection column

Here is my query :

// Table Name : Question
  Cursor cursor = db.query("QUESTION", new String[] { "TEXT", "OPTION_A",
    "OPTION_B", "OPTION_C", "OPTION_D", "OPTION_E",
    "RIGHT_ANSWER" },
    "SUBJECT=?", new String[] { "Ingles", "Historia" }, null,null,
    null);

I can have multiple subjects, right now i am having only two subjects(Ingles,Historia)

How to sort data by subjects using query??

I am getting bind or out of range error. I know error is somewhere in where clause , but not able to solve it.

Upvotes: 4

Views: 3052

Answers (2)

Jaiprakash Soni
Jaiprakash Soni

Reputation: 4112

You are getting this error because number of "?" in where and array length in arguments mismatch. You can put condition like this
1) Using two conditions in where

Cursor cursor = db.query("QUESTION", new String[] { "TEXT", "OPTION_A", "OPTION_B", "OPTION_C", "OPTION_D", "OPTION_E", "RIGHT_ANSWER" }, "SUBJECT1=? AND SUBJECT2=? ", new String[] { "Ingles", "Historia" }, null,null, "SUBJECT");
2) Using IN condition
Cursor cursor = db.query("QUESTION", new String[] { "TEXT", "OPTION_A", "OPTION_B", "OPTION_C", "OPTION_D", "OPTION_E", "RIGHT_ANSWER" }, "SUBJECT IN(?)", new String[] { "Ingles,Historia" }, null,null, "SUBJECT");

Upvotes: 2

moDev
moDev

Reputation: 5258

I got the answer.

1) To sort the data, need to enter column name in last argument

2) When multiple selection condition is available in one column use IN Operator.

 Cursor cursor = db.query("QUESTION", new String[] { "TEXT", "OPTION_A",
            "OPTION_B", "OPTION_C", "OPTION_D", "OPTION_E",
            "RIGHT_ANSWER" },
            "SUBJECT IN(?,?)", new String[] { "Ingles", "Historia" }, null,null,
            "SUBJECT");

Upvotes: 4

Related Questions