编写sqlite.query的正确方法是什么? (机器人)

时间:2016-04-15 04:24:38

标签: android sqlite

我正在努力更新和查询我的数据库中的某些条目。 上面的代码将JobNo与db中的条目进行比较并更新相应的条件,但是当尝试仅更新该特定组件时,第二行会引发错误。

     long id = db.update(DatabaseHelper.COMP_TABLE, contentValues, DatabaseHelper.HCJOBNO + " = '" + HCJobNo + "'" , null);
     long id = db.update(DatabaseHelper.COMP_TABLE, contentValues,  DatabaseHelper.HCJOBNO + " = " + HCJobNo + " AND " + DatabaseHelper.COMPONENTTAG + " = " + ComponentTag , null);

Simliar查询从数据库中获取数据时 它抛出错误。第一行有效,另外两行不正确

Cursor cursor = db.query(DatabaseHelper.COMP_TABLE, columns, DatabaseHelper.HCJOBNO + " = '" + JobNumber + "'", null, null, null, null);
Cursor cursor = db.query(DatabaseHelper.CONDITION_TABLE, columns, DatabaseHelper.CONDITIONJOBNO + " =? AND " + DatabaseHelper.PARTTAG + " =?", selectionArgs, null, null, null, null);
Cursor cursor = db.query(DatabaseHelper.COMP_TABLE, columns, DatabaseHelper.HCJOBNO + " = " + JobNumber + " AND " + DatabaseHelper.COMPONENTTAG + " = " + CompTag, null, null, null, null);

编写此特定代码的正确方法是什么?

Cursor cursor = db.query(DatabaseHelper.COMP_TABLE, columns, DatabaseHelper.HCJOBNO + " = " + JobNumber + " AND " + DatabaseHelper.COMPONENTTAG + " = " + CompTag, null, null, null, null);

编辑: 干杯

long id = db.update(DatabaseHelper.COMP_TABLE, contentValues,  DatabaseHelper.HCJOBNO + " = '" + HCJobNo + "' AND " + DatabaseHelper.COMPONENTTAG + " = '" + ComponentTag + "'", null);

1 个答案:

答案 0 :(得分:4)

Cursor cursor = db.query(DatabaseHelper.COMP_TABLE, columns, DatabaseHelper.HCJOBNO + " = " + JobNumber + " AND " + DatabaseHelper.COMPONENTTAG + " = " + CompTag, null, null, null, null);

在这段特殊的代码中乍看之下,你会错过变量周围的撇号 - 就像你在第一个字符串中所做的那样。

" = '" + JobNumber + "'"

或者你已经将它们包含在变量中了吗?值?