2012-04-15 53 views
0

我沒有很多運氣來搞清楚如何使用int和字符串進行查詢。我發現了兩個整數的例子,並且我找到了兩個字符串的例子。我嘗試了不同的組合,似乎沒有任何工作。以下是我嘗試過並沒有奏效的兩個例子。我究竟做錯了什麼。我自己使用了inpspection_id和item_name,它們都工作。使用int和字符串嘗試查詢

嘗試1:

public int getId(int inspection_id ,String item_name)throws Exception 
{ 
    Cursor c = db.query(DB_TABLE, new String[] {KEY_INSPECTION_ID, KEY_ITEM_NAME}, 
      "inspection_id=" + inspection_id +"item_name='"+ item_name + "'", null, null, null, null); 
    int id=c.getColumnIndex(KEY_ROWID); 
    c.moveToFirst(); 
    String result=c.getString(id); 
    int primId=Integer.parseInt(result); 
    c.close(); 
    return primId; 

} 

嘗試2:

public int getId(int inspection_id ,String item_name)throws Exception 
{ 
    Cursor c = db.query(DB_TABLE, new String[] {KEY_INSPECTION_ID, KEY_ITEM_NAME}, 
      "inspection_id=" + inspection_id +"AND KEY_ITEM_NAME = ?", new String[]{item_name}, null, null, null); 
    int id=c.getColumnIndex(KEY_ROWID); 
    c.moveToFirst(); 
    String result=c.getString(id); 
    int primId=Integer.parseInt(result); 
    c.close(); 
    return primId; 

} 

嘗試3:

public int getId(int inspection_id ,String item_name)throws Exception 
{ 
    String s_id= Integer.toString(inspection_id); 
    Cursor c = db.query(DB_TABLE, new String[] {KEY_INSPECTION_ID, KEY_ITEM_NAME}, 
      "KEY_INSPECTION_ID = ? AND KEY_ITEM_NAME = ?", 
      new String[] { s_id, item_name }, null, null, null); 
    int id=c.getColumnIndex(KEY_ROWID); 
    c.moveToFirst(); 
    String result=c.getString(id); 
    int primId=Integer.parseInt(result); 
    c.close(); 
    return primId; 

} 

回答

2

嘗試這種方式,用單引號爲INT的。

"inspection_id='" + inspection_id +"'item_name='"+ item_name + "'" 
+0

我試過了,它沒有工作 – Aaron 2012-04-15 20:46:14

+0

感謝它竟然是別的東西,但當我改變它的評論工作 – Aaron 2012-04-15 21:57:20