2

I am having a problem with the IN Operator I tried running this query

public Cursor getFoodToAvoid(String[] diseases){

    String value="";
    for(int i=0; i<diseases.length; i++){
        if(i!=diseases.length-1){
            value = value+diseases[i] +",";
        }else{
            value=value+diseases[i];
        }
    }
    Cursor c =null;
    String[] args={value};
    String sql = "SELECT distinct  "+FoodsTable.FOOD_NAME+", "+FoodsTable.FOOD_ENERGY_CONTENT+
            ","+FoodsTable.FOOD_ID+","+FoodsTable.FOOD_AMOUNT+","+FoodsTable.FOOD_CALORIES+ 
            ","+ FoodsTable.BARCODE+","+FoodsTable.FOOD_CATEGORY+ 

            " FROM "+FoodToAvoidTable.TABLE_NAME + " , " +FoodsTable.TABLE_NAME+
            " WHERE "+FoodToAvoidTable.FOOD_ID_FK+  " = "+FoodsTable.FOOD_ID + " AND " +
            FoodToAvoidTable.DISEASE_ID_FK+ " IN ( ? ) ";
    try{
        c = db.rawQuery(sql, args);
        Log.d(TAG, args[0] +" ");
    }catch(Exception e){
        e.printStackTrace();
    }
    return c;
}

But I am getting an empty cursor I know that the problem is with the IN operator but I can't work it out. I can't use the OR operator because the array diseases is dynamic and its size will change depending on the user

Thanks

  • 1
    `?` represents exactly *one* value binding, even in an `IN` clause - see [this answer of mine](http://stackoverflow.com/questions/7418849/android-sqlite-in-clause-and-placeholders/7419062#7419062) for a solution to what I suspect the problem is. That is, the individual values should be added to the `args` *array* and the placeholders should be created dynamically based on the length. –  Feb 15 '13 at 20:52
  • 1
    thank you so much it worked like a charm :D I don't know how I missed that question – amateur programmer Feb 15 '13 at 21:32

0 Answers0