我在编辑时更新记录时遇到问题。我认为它在DatabaseHandler上,因为我不知道如何更新数据库的正确代码。有人可以帮我解决这个问题吗?
btn_Update.setOnClickListener(new OnClickListener() {
@Override
public void onClick(View arg0) {
String ReportCode = tv_Code.getText().toString();
//Casting and conversion for district code
String District = spn_District.getSelectedItem().toString();
Cursor rDistrict = databaseHandler.getReport_DistrictCode(District);
String DistrictCode = rDistrict.getString(rDistrict.getColumnIndex(Constants.DISTRICT_CODE));
//Casting and conversion for province id
String Province = spn_Province.getSelectedItem().toString();
Cursor rProvince = databaseHandler.getReport_ProvinceId(Province);
String ProvinceCode = rProvince.getString(rProvince.getColumnIndex(Constants.PROVINCE_ID));
//Casting and conversion for InfoType id
String InfoType = spn_InfoType.getSelectedItem().toString();
Cursor rInfoType = databaseHandler.getReport_InfoTypeCode(InfoType);
String InfoTypeCode = rInfoType.getString(rInfoType.getColumnIndex(Constants.INFORMATIONTYPE_CODE));
int IsCompetitor = chkString;
String DateObserved = txt_Date.getText().toString();
String Remarks = txt_Remarks.getText().toString();
//UPDATE IREPORT
databaseHandler.UpdateReport(new Cons_iReport (ReportCode, InfoTypeCode, DistrictCode, ProvinceCode, DateObserved, IsCompetitor, Remarks));
Toast.makeText(getApplicationContext(), "UPDATE!", Toast.LENGTH_SHORT).show();
}
});
DatabaseHandler.java
public void UpdateReport(Cons_iReport save){
SQLiteDatabase db = this.getWritableDatabase();
ContentValues values = new ContentValues();
values.put(Constants.REPORT_CODE, save.getReportCode()); // Save Report Code
values.put(Constants.REPORT_DISTRICTCODE, save.getDistrict()); // Save District
values.put(Constants.REPORT_PROVINCECODE, save.getProvince()); // Save Province
values.put(Constants.REPORT_ISCOMPETITOR, save.getCompetitor()); // Save isCompetitor
values.put(Constants.REPORT_INFOTYPECODE, save.getInfoType()); // Save infoType
values.put(Constants.REPORT_DATEOBSERVED, save.getDateObserved()); // Save Date Observed
values.put(Constants.REPORT_REMARKS, save.getRemarks()); // Save Remarks
db.update(Constants.TABLE_REPORT, values, Constants.REPORT_CODE+" = ?", null);
db.close();
}
答案 0 :(得分:1)
您的whereArgs
是null
db.update(Constants.TABLE_REPORT, values, Constants.REPORT_CODE+" = ?", null);
您需要提供它们,否则它将找不到任何要更新的记录。
它相当于说“更新Constants.REPORT_CODE等于......没有的记录”。您需要告诉它要更新的REPORT_CODE。