moDev
moDev

Reputation: 5258

How to use Multiple values from Multiple column in sqlite using query?

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.

Upvotes: 0

Views: 5588

Answers (2)

user2895020
user2895020

Reputation: 1

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);

Upvotes: 0

waqaslam
waqaslam

Reputation: 68177

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});

Upvotes: 2

Related Questions