2013-01-23 34 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

相关问题