0

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?

2
  • You mean you have a list of names, and you want to get all results that match one of those names? Commented Sep 24, 2018 at 9:10
  • @MarkusPenguin oh sorry, I want to search the list of names Commented Sep 24, 2018 at 9:13

5 Answers 5

2

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')
Sign up to request clarification or add additional context in comments.

Comments

2

You can use IN condition

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

Comments

0

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

Comments

0

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

Comments

0

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.

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.