Search code examples
javaandroidsqliteandroid-sqlite

SQLite Android multiple selectionArgs for one selection parametr


I now there are similar questions here, but I can't figure out what to do by them, I got following code

 Cursor getterCursor = null;

    String selection = "topic" + " =? ";
    String[] selectionArgs = new String[] {"topic1", "topic2"};

    getterCursor = questionsDatabase.query(mDBName, null, selection, selectionArgs, null, null, null);

And it returns an error java.lang.IllegalArgumentException: Cannot bind argument at index 2 because the index is out of range. The statement has 1 parameters.. I understand that it happens because I got two elements in array selectionArgs, but I have completely no idea how to make it work right, help please. One more time: I really tried to solve it by myself following the guides from internet and from SO in particular, but I could not handle it, so now asking for help here. Thanks in advance!


Solution

  • If you want results for 2 vaues of topic then use the operator IN:

    String selection = "topic IN (?, ?)";
    

    This is equivalent to:

    String selection = "topic = ? OR topic = ?";