使用int和string尝试查询

时间:2012-04-15 20:33:05

标签: android sqlite

我没有很多运气搞清楚如何使用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;

}

1 个答案:

答案 0 :(得分:2)

以这种方式尝试,使用int的单引号。

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