我创建了一个SQLite数据库并使用 execSQL()方法更新了表格中的一行。我使用的函数运行没有错误,但不会发生更新。
这是我的更新功能:
public void updateSalesRecovery(Sales_Recovery man) {
SQLiteDatabase db = this.getWritableDatabase();
String query = "UPDATE " + TABLE_SALES_RECOVERY + " SET " +
SR_KEY_EDATE + " = '" + man.getEDATE() + "' , " +
SR_KEY_AUTON + " = " + man.getAUTON() + " , " +
SR_KEY_INVDT + " = '" + man.getINVDT() + "' , " +
SR_KEY_INVAM + " = " + man.getINVAM() + " , " +
SR_KEY_INVBL + " = " + man.getINVBL() + " , " +
SR_KEY_TYPER + " = '" + man.getTYPER() + "' , " +
SR_KEY_SLNAM + " = '" + man.getSLNAM() + "' , " +
SR_KEY_SRNO + " = " + man.getSRNO() + " , " +
SR_KEY_PANAM + " = '" + man.getPANAM() + "' , " +
SR_KEY_PADR1 + " = '" + man.getPADR1() + "' , " +
SR_KEY_PADR2 + " = '" + man.getPADR2() + "' , " +
SR_KEY_PADR3 + " = '" + man.getPADR3() + "' , " +
SR_KEY_PADR4 + " = '" + man.getPADR4() + "' , " +
SR_KEY_DLNAM + " = '" + man.getDLNAM() + "' , " +
SR_KEY_MNAME + " = '" + man.getMNAME() + "' , " +
SR_KEY_CNAME + " = '" + man.getCNAME() + "' , " +
SR_KEY_RNAME + " = '" + man.getRNAME() + "' , " +
SR_KEY_CRNOT + " = '" + man.getCRNOT() + "' , " +
SR_KEY_CRAMT + " = " + man.getCRAMT() + " , " +
SR_KEY_RTAUT + " = " + man.getRTAUT() + " , " +
SR_KEY_RTAMT + " = " + man.getRTAMT() + " , " +
SR_KEY_SLCOD + " = " + man.getSLCOD() + " , " +
SR_KEY_DLCOD + " = " + man.getDLCOD() + " , " +
SR_KEY_ARCOD + " = " + man.getARCOD() + " , " +
SR_KEY_PACOD + " = " + man.getPACOD() + " , " +
SR_KEY_CHQAM + " = " + man.getCHQAM() + " , " +
SR_KEY_CSHAM + " = " + man.getCSHAM() + " , " +
SR_KEY_RETAM + " = " + man.getRETAM() +
" WHERE " + SR_KEY_AUTON + " = " + man.getAUTON();
db.execSQL(query);
}
我也用这个函数来更新行:
public int updateSalesRecovery(Sales_Recovery man) {
SQLiteDatabase db = this.getWritableDatabase();
ContentValues values = new ContentValues();
values.put(SR_KEY_AUTON, man.getAUTON()); // SALE AUTO NUMBER
values.put(SR_KEY_SRNO, man.getSRNO()); // SALE SERIAL NUMBER
values.put(SR_KEY_EDATE, man.getEDATE()); // SALE DATE
values.put(SR_KEY_INVDT, man.getINVDT());
values.put(SR_KEY_INVAM, man.getINVAM());
values.put(SR_KEY_INVBL, man.getINVBL());
values.put(SR_KEY_TYPER, man.getTYPER());
values.put(SR_KEY_PANAM, man.getPANAM());
values.put(SR_KEY_PADR1, man.getPADR1());
values.put(SR_KEY_PADR2, man.getPADR2());
values.put(SR_KEY_PADR3, man.getPADR3());
values.put(SR_KEY_PADR4, man.getPADR4());
values.put(SR_KEY_SLNAM, man.getSLNAM());
values.put(SR_KEY_DLNAM, man.getDLNAM());
values.put(SR_KEY_MNAME, man.getMNAME());
values.put(SR_KEY_CNAME, man.getCNAME());
values.put(SR_KEY_RNAME, man.getRNAME());
values.put(SR_KEY_CRNOT, man.getCRNOT());
values.put(SR_KEY_CRAMT, man.getCRAMT());
values.put(SR_KEY_RTAUT, man.getRTAUT());
values.put(SR_KEY_RTAMT, man.getRTAMT());
values.put(SR_KEY_SLCOD, man.getSLCOD());
values.put(SR_KEY_DLCOD, man.getDLCOD());
values.put(SR_KEY_CHQAM, man.getCHQAM());
values.put(SR_KEY_CSHAM, man.getCSHAM());
values.put(SR_KEY_RETAM, man.getRETAM());
values.put(SR_KEY_ARCOD, man.getARCOD());
values.put(SR_KEY_PACOD, man.getPACOD());
// updating row
return db.update(TABLE_SALES_RECOVERY, values, SR_KEY_AUTON + " = ?",
new String[]{String.valueOf(man.getAUTON())});
}
这就是我在oncreate()类中检查我的数据库的方式(我知道它有点矫枉过正):
DBHandler dbHandler = new DBHandler(context);
Sales_Recovery sale = dbHandler.getSalesRecovery("1557");
System.out.println("AUTON: " + sale.getAUTON());
System.out.println("PANAM: " + sale.getPANAM());
System.out.println("CSHAM: " + sale.getCSHAM());
System.out.println("CHANGING NOW!!");
sale.setCSHAM("522");
System.out.println("CSHAM CHANGED");
System.out.println(sale.getCSHAM());
System.out.println("Updating");
dbHandler.updateSalesRecovery(sale);
System.out.println("Updated");
Sales_Recovery saleU = dbHandler.getSalesRecovery("1557");
System.out.println("AUTON: " + saleU.getAUTON());
System.out.println("PANAM: " + saleU.getPANAM());
System.out.println("CSHAM: " + saleU.getCSHAM());
System.out.println("Printing updated field should be 522");
这是我收到的输出:
03-06 13:05:28.899 30046-30046/com.example.tahir.rhm I/System.out: AUTON: 1557
03-06 13:05:28.899 30046-30046/com.example.tahir.rhm I/System.out: PANAM: THE INDUS HOSPITAL
03-06 13:05:28.899 30046-30046/com.example.tahir.rhm I/System.out: CSHAM: 500
03-06 13:05:28.899 30046-30046/com.example.tahir.rhm I/System.out: CHANGING NOW!!
03-06 13:05:28.899 30046-30046/com.example.tahir.rhm I/System.out: CSHAM CHANGED
03-06 13:05:28.899 30046-30046/com.example.tahir.rhm I/System.out: 522
03-06 13:05:28.899 30046-30046/com.example.tahir.rhm I/System.out: Updating
03-06 13:05:28.899 30046-30046/com.example.tahir.rhm I/System.out: AUTON: 1557
03-06 13:05:28.899 30046-30046/com.example.tahir.rhm I/System.out: CSHAM: 522
03-06 13:05:28.909 30046-30046/com.example.tahir.rhm I/System.out: Updated
03-06 13:05:28.909 30046-30046/com.example.tahir.rhm I/System.out: AUTON: 1557
03-06 13:05:28.909 30046-30046/com.example.tahir.rhm I/System.out: PANAM: THE INDUS HOSPITAL
03-06 13:05:28.909 30046-30046/com.example.tahir.rhm I/System.out: CSHAM: 500
修改 简化查询:
UPDATE SALES_RECOVERY SET EDATE = '2015-08-24 00:00:00.0' , AUTON = 1557 , INVDT = '2015-07-03 00:00:00.0' , INVAM = 1285 , INVBL = 1285 , TYPER = 'N' , SLNAM = 'MOHAMMAD KALEEM' , SRNO = 1 , PANAM = 'THE INDUS HOSPITAL' , PADR1 = 'C/O CHILDREN-CANCER HOSPITAL' , PADR2 = 'KORANGI CROSSING, KORANGI' , PADR3 = 'KARACHI' , PADR4 = 'MR. ADNAN (P) 0306 2727355.' , DLNAM = 'SELF' , MNAME = 'BILL DUE ON 02-08-2015' , CNAME = 'null' , RNAME = 'null' , CRNOT = 'null' , CRAMT = null , RTAUT = null , RTAMT = null , SLCOD = 20 , DLCOD = null , ARCOD = 12 , PACOD = 641 , CHQAM = 500 , CSHAM = 522 , RETAM = 100 WHERE AUTON = 1557
非常感谢任何帮助,我们非常感谢你!