0

I have database with three columns. I would like to query the database, based on one column which can have multiple values.

For single parameter we can the normal query method where cardName is String[]

 Cursor cursor = database.query(Database.TABLE_COUPON_CARD, allColumns,Database.COLUMN_CARD_NAME + " = ?", cardName, null, null,null);

but if there are more than one value, I get a Android SQLite cannot bind argument exception

For multiple values of the same column we can use IN statement but, here how do I write the QUERY or how should i form the rawQuery

String whereClause = Database.COLUMN_CARD_NAME+ " IN(?)";
         Cursor cursor = database.query(Database.TABLE_COUPON_CARD, allColumns,whereClause,new String[][]{cardName}, null, null,null);

Android QUERY doesnot take array of array. What should the correct query be?

TEMPORARY SOLUTION Currently I have created a method which dynamically creates the clause.

 private static StringBuilder buildInClause(String[] myStringArray){
    StringBuilder fullString=new StringBuilder();
    fullString.append("(");
    for(int i=0;i<myStringArray.length;i++){
        fullString.append(" '"+myStringArray[i]+"' ");
        if(i!=myStringArray.length-1){
            fullString.append(",");
        }

    }
    fullString.append(")");
    return fullString;

}

If anyone has any other solution please do share.

1 Answer 1

1

For two values: IN(?,?). For three values: IN(?,?,?). Get the idea? Each ? corresponds to a single literal in the selection args array.

Sign up to request clarification or add additional context in comments.

1 Comment

In clause will need to be generated dynamically, based on no of values being sent. in(?) when passing one value, in(?,?) when passing two values

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.