2012-04-15 52 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