I do a search in the application, in the SelectionArgs array, the names of the necessary books, what do I need to specify in the selection in order to display the necessary books?

String or = " OR ?"; String selection = "B_NAME = ?"; for (int i = 0; i< selectionArgs.length; i++){ selection += or; } Cursor c = myDbHelper.query1("BOOK_TABLE_ALL", null, selection, selectionArgs, null, null, null); if (c.moveToFirst()) { do { Toast.makeText(this, c.getString(1), Toast.LENGTH_SHORT).show(); } while (c.moveToNext()); } 

I do so, displays only the first element.

    3 answers 3

    Invalid request from you.

    Correctly:

     select * from BOOK_TABLE_ALL where B_NAME in (?,?,?) 

    or

     select * from BOOK_TABLE_ALL where B_NAME=? or B_NAME=? or B_NAME=? 

    For the first option will be something like this:

     StringBuilder selection = new StringBuilder(); for (int i = 0; i < selectionArgs.length; i++){ if(selection.length()>0) selection.append(","); selection.append("?"); } selection.insert(0, "B_NAME in ("); selection.append(")"); dbHelper.query(..., selection.toString(), ...); 

      In my opinion should work with the condition OR , provided that the arguments> 0

       StringBuilder selection = new StringBuilder(); selection.append("SELECT * FROM BOOK_TABLE_ALL WHERE "); String b_name = " B_NAME = ? "; String or = " OR "; if(selectionArgs.length > 1){// аргументов > 1 for (int i = 0; i< selectionArgs.length - 1; i++){ selection.append(b_name).append(or); } } selection.append(b_name);//аргумент только 1 или > 1 Cursor c = myDbHelper.rawQuery(selection.toString(), selectionArgs); 

        Try to change the part:

         String or = " OR ?"; String selection = "B_NAME = ?"; for (int i = 0; i< selectionArgs.length; i++){ selection += or; } 

        on:

         String or = " OR "; String template = "B_NAME=?"; String selection = "B_NAME=?"; for (int i = 0; i< selectionArgs.length - 1; i++){ selection += or + template; }