0

Using the code below i can check if a row contains a single string ( String a) but how would i check if a row equals either string (String a or b)?

public Cursor fetchMyList() {
        String[] columns = { KEY_ROWID, KEY_CATEGORY, KEY_SUMMARY,
                KEY_DESCRIPTION, KEY_EMAIL };
        String selection = "description=?";
        String a = "blue";
                String b = "red";

        String[] selectionArgs = { a };
               // String[] selectionArgs = { a , b}; ///tried this dont work!!
        Cursor cursor = null;
        try {
            cursor = database.query(DATABASE_TABLE, columns, selection,
                    selectionArgs, null, null, null);
        } catch (Exception e) {
            e.printStackTrace();
        }
        int numberOfRows = cursor.getCount();
        if (numberOfRows <= 0) {
            return cursor;
        }
        return cursor;
    }
steo
  • 293
  • 1
  • 4
  • 15

1 Answers1

0

You can only pass 2 arguments if you declare 2 arguments. This is what you want:

String selection = "description=? OR description=?"; // Select rows with either description
String[] selectionArgs = {a , b};

I strongly suggest you check SQL language.

PS: do not catch Exception. You'll regret it later. Catch specifc Exception children; in your case you want to catch SQLException.

PS2: use Log instead of printStackTrace().

m0skit0
  • 25,268
  • 11
  • 79
  • 127
  • Thank you so much I've spent hours searching for this answer, is there a reason I shouldn't be using Exception? – steo Jan 23 '13 at 19:06
  • 1
    You're welcome. http://stackoverflow.com/questions/2416316/why-is-the-catchexception-almost-always-a-bad-idea – m0skit0 Jan 23 '13 at 19:07