Reputation: 1903
I am trying to do a search function in android that will allow the user search with different filters. I am able to create a simple select
statement with multiple where
clauses. My issue arises when the user doesnt use the filter options, is there a way to continue the select statement but use blank values in the where
clause?
For example, the user will want to search for all the cars and add a filter looking for specific green cars made in the year 2011.
"Select * from cars where colour = 'green' AND year = 2011"
Example where the user searches without filters for all cars
"Select * from cars where colour = '' AND year = "
I dont want to have to use two statements I would just like to use the one and have blanks in the where
clause is nothing is selected in the filter, is this possible?
Upvotes: 0
Views: 69
Reputation: 1777
You can do with a simple method, for example:
public void searchMethod(boolean filtered, colourString, yearValue) {
String selectQuery = "";
if (filtered)
selectQuery = "Select * from cars where colour = " + colourString + " AND year = " + yearValue;
else
selectQuery = "Select * from cars";
Cursor c = mDb.rawQuery(selectQuery, null);
...
}
Upvotes: 1
Reputation: 722
u can write somelogic like this:-
String whereClause = "WHERE colour = 'green' AND year = 2011';
String query = "SELECT * FROM cars ";
if(isForFilter) { //booelan to check if result is based on filter
query = query + whereClause;
}
Upvotes: 0