MOHS3N
MOHS3N

Reputation: 247

sqlite query to search database with multiple keywords

I used a code similar to the following code to search the database person name :

cursor.query(select column from Table where column_person_Name="John");

But now I want to search the names of a few people together,How to do it?

Upvotes: 0

Views: 3340

Answers (5)

Abu Yousuf
Abu Yousuf

Reputation: 6142

Room supports Collections as a query argument.

Query in Room is like:

@Query("SELECT first_name, last_name FROM user WHERE region IN (:regions)")
public List<NameTuple> loadUsersFromRegions(List<String> regions);

Check official guides.

Upvotes: 0

Markus Penguin
Markus Penguin

Reputation: 1661

The way to do that using SQlite would be using IN:

SELECT * FROM persons WHERE name IN ("John", "Doe");

In Java, having an array of Strings, you could do it like that:

final String[] namesArray = {"John", "Doe"};

// Pad the Strings with quotes: John -> 'John'
for (int i = 0; i < namesArray.length; i++) {
    namesArray[i] = "'" + namesArray[i] + "'";
}

// Join the Strings: 'John', 'Doe'
final String names = TextUtils.join(",", namesArray);
final String query = "SELECT * FROM persons WHERE name IN ("+names+")";

This results in:

SELECT * FROM persons WHERE name IN ('John','Doe')

Upvotes: 2

Tejas
Tejas

Reputation: 443

Other solution for your problem can be, use like Keyword, where you get multiple results based on the search.

Like is used across application partial known data.

 SELECT
   column_list
FROM
   table_name
WHERE
  column_1 LIKE %yoursearch_word%;

Upvotes: 0

Prasanth S
Prasanth S

Reputation: 3830

You can use IN condition

SELECT *
FROM Table
WHERE column_person_Name IN ('Name1', 'Name2', 'Name3');

Upvotes: 2

kelalaka
kelalaka

Reputation: 5646

By using the OR operator;

SELECT column1, column2, ...
FROM table_name
WHERE condition1 OR condition2 OR condition3 ...;

here an example;

cursor.rawQuery("select column from Table where column_person_Name='John' OR column_person_Name='Bob');

Upvotes: 0

Related Questions