2010-08-13 50 views
0

我想查詢一個詞,爲此我使用db.query方法。但我想使用WHERE子句。我在代碼上做了這樣的事情,但我想我的WHERE子句有問題。如何實現Android Query方法的where子句?

public String select(String wrd) { 
    String list = new String(); 
    Cursor cursor = this.db.query(TABLE_NAME, new String[] {"word"}, 
    "word like " + wrd + "", null, null, null, "id desc"); 
    if (cursor.moveToNext()) { 
    do { 
     list = cursor.getString(0); 
    } while (cursor.moveToNext()); 
    } 
    if (cursor != null && !cursor.isClosed()) { 
    cursor.close(); 
    } 
    return list; 

}

我打電話給在其他類此方法,解析字符串參數。

Cursor cursor = getContentResolver().query(CONTENT_URI, null, null, null, null);          
     String body;           
     if(cursor.moveToFirst()){ 
      body = cursor.getString(cursor.getColumnIndexOrThrow("body")).toString(); 
      if(body == ""){ 
       Toast.makeText(getBaseContext(), "There is no words to save!", Toast.LENGTH_LONG).show(); 
      } 
       else{ 
        String words = this.dh.select("Testando"); 
        StringTokenizer st = new StringTokenizer(body); 
        while(st.hasMoreTokens()){ 
         if(words == st.nextToken()) 
         Toast.makeText(getBaseContext(), "found! "+words+"", Toast.LENGTH_LONG).show(); 
         this.dh.insert(st.nextToken());        
         Toast.makeText(getBaseContext(), "The set of words has been updated!", Toast.LENGTH_LONG).show();                            
        }      
        StringBuilder sb = new StringBuilder(); 
        sb.append("Set of words:\n\n"); 
        sb.append(words + " ");      
        txtView.setText(sb.toString()); 
       } 
     } 
+0

您遇到的錯誤是什麼? – Asahi 2010-08-13 20:54:28

+0

「模擬器返回錯誤」 - 您應該包含錯誤消息,然後... – poke 2010-08-13 20:54:36

回答

2

我相信這個問題已經與下面代碼的雙引號的事:

Cursor cursor = this.db.query(TABLE_NAME, new String[] {"word"}, 
    "word like " + wrd + "", null, null, null, "id desc"); 

試試這個

Cursor cursor = this.db.query(TABLE_NAME, new String[] {"word"}, 
    "word like \"" + wrd + "\"", null, null, null, "id desc"); 

甚至更​​好

Cursor cursor = this.db.query(TABLE_NAME, new String[] {"word"}, 
     "word like ?",new String[] {wrd} , null, null, "id desc"); 

編輯:

在您的WHERE子句中使用單引號而不是雙引號。請參閱here

Cursor cursor = this.db.query(TABLE_NAME, new String[] {"word"}, 
    "word like \'" + wrd + "\'", null, null, null, "id desc");