我没有太多运气弄清楚如何使用 int 和字符串进行查询。我找到了两个整数的例子,我找到了两个字符串的例子。我尝试了不同的组合,但似乎没有任何效果。以下是我尝试过但没有奏效的两个示例。我究竟做错了什么。我自己使用了 inpspection_id 和 item_name ,它们都可以工作。
尝试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;
}