Home > database >  Howto make SELECT queries with variable size for filtering parameters?
Howto make SELECT queries with variable size for filtering parameters?

Time:11-09

With RoomBD I could make in a DAO a query like this one:

@Query("SELECT * FROM table "
       "WHERE field1 LIKE :param1 "
       "OR field1 LIKE :param2 "
       "OR field1 LIKE :param3")
public LiveData<List<Table>> filterTableData(String param1, String param2, String param3);

...but the point is that i need to do this kind of SELECT statement with a variable amount of filtering parameters, so I don't have to redundantly create multiple filterTableData methods in the DAO that would do exactly the same thing but with a different amount of filtering parameters. Is there a way to do so?

// XXX What I'd like it to be (SQL is pseudocode here)
@Query("SELECT * FROM table WHERE field1 LIKE :any_of_the_parameters")
public LiveData<List<Table>> filterTableData(ArrayList<String> params);

CodePudding user response:

I haven't tested this yet, but according to this link, this might do the job for OR-like filtering:

@Query("SELECT * FROM table WHERE field1 IN(:params)")
LiveData<List<Table>> filterTableData(List<String> params);

However, I don't have any idea for AND-like filtering (yet)...

CodePudding user response:

Create the query programatically with the list of parameters.

  • Related