2013-01-23 36 views
0

使用下面的代碼我可以檢查一行是否包含單個字符串(字符串a),但我將如何檢查一行是否等於任何字符串(字符串a或b)?Sqlite,如何檢查一行是否包含字符串

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; 
    } 

回答

0

如果聲明2個參數,則只能傳遞2個參數。這是你想要的:

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

我強烈建議你檢查SQL language

PS:別抓Exception。你會後悔的。捕捉指定異常兒童;在你的情況下,你想趕上SQLException

PS2:使用Log而不是printStackTrace()

+0

非常感謝你我花了數小時尋找這個答案,是否有我不應該使用異常的原因? – steo

+1

不客氣。 http://stackoverflow.com/questions/2416316/why-is-the-catchexception-almost-always-a-bad-idea – m0skit0

相關問題