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.

moDev
  • 5,248
  • 4
  • 33
  • 63

2 Answers2

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});
waqaslam
  • 67,549
  • 16
  • 165
  • 178
  • i want to use multiple columns not just one column – moDev Aug 29 '12 at 08:39
  • then do the same with multiple columns using `OR` keyword. see my updated answer – waqaslam Aug 29 '12 at 09:25
  • 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?? – moDev Aug 29 '12 at 10:06
  • can you try my another question? Here is the link http://stackoverflow.com/questions/12156849/live-wallpaper-getting-force-close-issue – moDev Aug 29 '12 at 11:54
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);