0

I'm trying to do a particular function for my code. Suppose, in my database, there is an entry called tandoori chicken. How do I code the SQL part so that I can filter the database with chicken tandoori and not just fixed on tandoori chicken?

public class MyDatabase extends SQLiteAssetHelper {

    private static final String DATABASE_NAME = "FoodDatabase1.sqlite";
    private static final int DATABASE_VERSION = 1;

    public MyDatabase(Context context) {
    super(context, DATABASE_NAME, null, DATABASE_VERSION);
}

And the getFood function.

/*
* Receive searchQuery in string form
* return Cursor object
*/
public Cursor getFood(String searchQuery) {

    SQLiteDatabase db = getReadableDatabase();
    SQLiteQueryBuilder qb = new SQLiteQueryBuilder();

    String [] sqlSelect = {"_id", "FOOD", "CALORIES"};
    String sqlTables = "fooddb1";

    String whereClause=null;
    String[] whereArgs=null;

    /*
    *if searchQuery is empty then null will be passed in query() function which 
    *will display all rows
    *if searchQuery is not null it will look for match in the table
    */

    if(!searchQuery.equals("")){
        whereClause="Food LIKE ?";

        /*
        *LIKE statement will look for substring in the table if found it will add that row to cursor
        */
        whereArgs= new String[] {
                "%"+searchQuery+"%"
        };
    }

    qb.setTables(sqlTables);

    Cursor c = qb.query(db, sqlSelect, whereClause, whereArgs,
            null, null, null);

    c.moveToFirst();
    return c;
}
Reaz Murshed
  • 23,691
  • 13
  • 78
  • 98

1 Answers1

0

You just need to split the substrings by space. For example, you've tandoori chicken as search query string. So, now you need to split the query string by space to make two separate words- tandoori and chicken.

Then the sql query should look like

Select * from foodTable where Food like 'tandoori chicken' or 'chicken tandoori'

To achieve this you might consider doing something like this.

String[] queryWords = searchQuery.split(" ");  // Split by space

Now make the words and put them in an ArrayList.

private ArrayList<String> getQueryStrings(String[] queryWords) {

    private ArrayList<String> queryStringList = new ArrayList<String>();
    // Now let us do some combination of words here and add each combination in the ArrayList.
    for(int i = 0; i < possibleCombinationCount; i++)
        queryStringList.add(getWordsCombination(i)); 

    return queryStringList; 
}

Now make the query string as you like.

String builder = "";

for(String wordsComb : getQueryStrings()) {
    // make the query with or 
    if(builder.length != 0) builder += " or ";
    builder += "'%" + wordsComb + "%'";
}

String query = "Select * from foodTable where Food like " + builder;

Now run the rawQuery() on your database.

db.rawQuery(query);

This solution may work well for two or three words in a string while it won't work well for long strings.

If your search is flexible like you just want to find the rows matched with the given strings you might consider using the IN statement.

Select * from foodTable where Food in (queryWords[0], queryWords[1], ....)

Just you need to build the database query of your own with the values separated from the query string by space in queryWords array.

I found this answer relevant to your question too.

Community
  • 1
  • 1
Reaz Murshed
  • 23,691
  • 13
  • 78
  • 98
  • Does this mean that for the queryWords array, i need to define all the possible combinations? – coffeeboi619 Jan 16 '17 at 16:59
  • For the solution with `IN` clause, you don't have to define all combinations. Just have to split them in the `queryWords` array. – Reaz Murshed Jan 16 '17 at 17:32
  • Can you perhaps give an example of how to split them in the queryWords array? Thanks! – coffeeboi619 Jan 17 '17 at 14:16
  • If the `searchQuery` is `chicken tandoori` then the `queryWords` array might contain `["chicken","tandoori"]`. – Reaz Murshed Jan 17 '17 at 14:25
  • I did what you told and created the array. However it seems to only be working for 2 words in a string. Like If i searched chicken and wrap, it comes up everything with chicken and wrap. But if i add a 3rd parameter, it doesn't work. How should i go about it? – coffeeboi619 Jan 17 '17 at 15:07
  • I managed to get the 3rd parameter work. But i was using these lines of code in my if statement. "%" + arr[0] + "%" + arr[1] + "%" + arr[2] + "%", "%" + arr[1] + "%" + arr[0] + "%" + arr[2] + "%" so it means that i can return searches with terms like chicken wrap cheese or wrap chicken cheese, but not cheese chicken wrap – coffeeboi619 Jan 17 '17 at 15:17
  • No, you're missing other combinations here like ` "%" + arr[0] + "%" + arr[2] + "%" + arr[1] + "%"` or ` "%" + arr[2] + "%" + arr[0] + "%" + arr[1] + "%"` etc. I think you should go for `IN` statement to avoid making these combinations. This is not a very good solution. – Reaz Murshed Jan 17 '17 at 15:22