0

I have five columns and i want to search data from five columns but column values can be more than 5, its not fixed.

Example

column name

first-name,last-name,subject,result,grade.

I want to search more-than one value using sqlite.(first-name can be a,b,c,d)

How to achieve this??

Any help would be appreciated.

2 Answers 2

2

Using IN keyword in SQLite would allow you perform search with a set of multiple values. For example:

SELECT * FROM table WHERE first-name IN ('a', 'b', 'c', 'd');

Now what you can do is to build an input (searchable words) parameter using any loop and replace it in the IN brackets.

For example:

String names = "'a', 'b', 'c', 'd'";      /* build it through loop */

Cursor c = db.rawQuery("SELECT * FROM table WHERE first-name IN (?)",
              new String[]{names});

With multiple columns:

String names = "'a', 'b', 'c', 'd'";      /* build it through loop */

Cursor c = db.rawQuery("SELECT * FROM table WHERE first-name IN (?)
         OR last-name IN (?) OR subject IN (?) OR result IN (?) OR grade IN (?)",
         new String[]{names, names, names, names, names});
Sign up to request clarification or add additional context in comments.

4 Comments

i want to use multiple columns not just one column
then do the same with multiple columns using OR keyword. see my updated answer
Its working for single value with multiple column,might be i am not using properly for multiple values. String names="'Zac',"Peter""; Is this ok?? how i use multiple string array in selectionArgs??
can you try my another question? Here is the link stackoverflow.com/questions/12156849/…
0
String[] names = new String[3];
String QueryArgs ="";   
for(int index=0;index < 3;index++)
{
      QueryArgs = QueryArgs.concat(",?");
      names [index] = "xyz";
}
QueryArgs = QueryArgs.substring(1);

Cursor dataset = db.rawquery("select * from table where name in(" +QueryArgs+ ")",names);

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.