使用字符串作为搜索参数时,是否需要不同的查询?

时间:2012-04-11 21:36:22

标签: android database sqlite

我正在查询我的数据库。当我使用int作为选择部分时,我得到了我想要的信息。当我使用字符串进行选择时,我得不到任何回报。使用字符串时是否有不同的查询方式或我做错了什么。当我传入一个int时,这个函数工作,但是当我传入一个字符串时,它不起作用。我使用断点并且参数正确进入函数,但它不从数据库中检索数据。

这是代码

 public String childFound(String item_name)throws Exception
{

    Cursor c = db.query(DB_TABLE, new String[] { KEY_INSPECTION_ID, KEY_ITEM_NAME},
            "item_name="+ item_name, null, null, null, null);

       int id=c.getColumnIndex(KEY_ITEM_NAME);
        c.moveToFirst();
        String result=c.getString(id);
       c.close();
       return result;

}

如果我将String item_name更改为int inspectionItem,则它可以正常工作。我还包括我为db声明我的变量的地方。

 public class InspectionRecordsTableDBAdapter {
private static final String KEY_ROWID = "_id";
private static final String KEY_INSPECTION_ID = "inspection_id";
private static final String KEY_PARENT_NAME = "parent_name";
private static final String KEY_ITEM_NAME = "item_name";
private static final String KEY_COMMENT = "comment";
private static final String KEY_STATUS = "status";
private static final String DB_TABLE = "inspection_records";
private Context context;
private SignalSetDBHelper dbHelper;
private SQLiteDatabase db;
private SQLiteOpenHelper openHelper;

如果您需要更多代码,请与我们联系。

1 个答案:

答案 0 :(得分:1)

"item_name="+ item_name

应该是

"item_name='"+ item_name + "'"

认为你错过了引号