编辑:[已解决]
更新方法从其调用方法获取了错误的参数。因此产出不如预期。感谢您指出更新语句可能没有任何匹配。
我编写了各种数据库更新方法,所有这些方法都正常工作,除了一个。我无法理解那里出了什么问题。
这是我的代码:
BaseDbAdapter:
protected int updateRow(String table, ContentValues initialValues,
String whereClause) {
if (Const.DEBUGGING_DB)
Log.d(Const.DEBUG, "Row in table " + table + " updated "
+ whereClause);
return mDb.update(table, initialValues, whereClause, null);
}
PetitionsTableDbAdapter:
public void updateStatus(String member_id, String e_pno, String pno, String status) {
ContentValues values = new ContentValues();
values.put(DatabaseHelper.SENT_SUPPORT, status);
String whereClause = DatabaseHelper.MEMBER_ID_KEY + " = " + member_id + " AND " + ""
+ DatabaseHelper.E_PETITION_NUMBER_KEY + " = " + e_pno + " AND " + ""
+ DatabaseHelper.PETITION_NUMBER_KEY + " = '" + pno + "'";
super.updateRow(DatabaseHelper.PETITIONS_TABLE, values, whereClause);
}
public String getStatus(String petition_number) {
String status = "";
String query = "SELECT " + DatabaseHelper.SENT_SUPPORT + " FROM " + DatabaseHelper.PETITIONS_TABLE
+ " WHERE " + DatabaseHelper.PETITION_NUMBER_KEY + " = '" + petition_number + "'";
c = super.query(query);
if ((c.equals(null) || c.getCount() == 0 || !c.moveToFirst())) {
} else {
c.moveToFirst();
status = c.getString(c.getColumnIndex(DatabaseHelper.SENT_SUPPORT));
}
return status;
}
服务代码:
mPetitionsTableDbAdapter = DatabaseHelper.get(getApplicationContext()).getPetitionsTableDbAdapter();
mPetitionsTableDbAdapter.beginTransaction();
try {
mPetitionsTableDbAdapter.updateStatus(member_id, e_petition_no, petition_no, "1");
mPetitionsTableDbAdapter.setTransactionSuccessful();
} catch (Exception e) {
e.printStackTrace();
} finally {
mPetitionsTableDbAdapter.endTransaction();
}
String status = mPetitionsTableDbAdapter.getStatus(petition_no);
Log.d(Const.DEBUG, "Status: " + status);
我的Logcat:
Begin Transaction
10-12 18:36:55.104 17865-19023/com.xxx.xx D/xx: Row in table petitions_table updated member_id = 2199683 AND e_petition_number = 31 AND petition_number = '31T8'
10-12 18:36:55.104 17865-19023/com.xxx.xx D/xx: Transaction Successful
10-12 18:36:55.104 17865-19023/com.xxx.xx D/xx: End Transaction
10-12 18:36:55.104 17865-19023/com.xxx.xx D/xx: Query 'SELECT sent_support FROM petitions_table WHERE petition_number = '31T8'' returned 1 rows
10-12 18:36:55.104 17865-19023/com.xxx.xx D/xx: Status: 0
我正在尝试将SENT_SUPPORT值设置为1,并在事务后读取它。它始终返回0,这是我在创建行时插入的默认值。如果您需要代码的任何其他部分,请告诉我。
注意:特定的petition_number只有一行。
答案 0 :(得分:1)
您可能需要在查询中转义'
之类的字符
有关详情,请查看问题How to escape special characters like ' in sqlite in android
答案 1 :(得分:0)
只是为了结束这个问题。
答案:更新方法从其调用方法获取了错误的参数。因此产出不如预期。感谢您指出更新语句可能没有任何匹配。