14

我需要更新某个表的列中的值。我试过这个:

 public void updateOneColumn(String TABLE_NAME, String Column, String rowId, String ColumnName, String newValue){
                 String sql = "UPDATE "+TABLE_NAME +" SET " + ColumnName+ " = "+newValue+" WHERE "+Column+ " = "+rowId;
                 db.beginTransaction();
                 SQLiteStatement stmt = db.compileStatement(sql);
                 try{
                     stmt.execute();
                     db.setTransactionSuccessful();
                 }finally{
                     db.endTransaction();
                 }

        }

我这样称呼这个方法:

 db.updateOneColumn("roadmap", "id_roadmap",id,"sys_roadmap_status_mobile_id", "1");

这意味着我想1在列sys_roadmap_status_mobile_id中设置值id_roadmap = id.

问题是什么都没有发生。我的错误在哪里?

4

2 回答 2

37

简单的解决方案:

String sql = "UPDATE "+TABLE_NAME +" SET " + ColumnName+ " = '"+newValue+"' WHERE "+Column+ " = "+rowId;

更好的解决方案:

ContentValues cv = new ContentValues();
cv.put(ColumnName, newValue);
db.update(TABLE_NAME, cv, Column + "= ?", new String[] {rowId});
于 2012-07-19T15:12:22.453 回答
1

以下解决方案适用于我更新单行值:

public long fileHasBeenDownloaded(String fileName) 
{
    SQLiteDatabase db = this.getWritableDatabase();

    long id = 0;

    try {
        ContentValues cv = new ContentValues();
        cv.put(IFD_ISDOWNLOADED, 1);

        // The columns for the WHERE clause
        String selection = (IFD_FILENAME + " = ?");

        // The values for the WHERE clause
        String[] selectionArgs = {String.valueOf(InhalerFileDownload.fileName)};

        id = db.update(TABLE_INHALER_FILE_DOWNLOAD, cv, selection, selectionArgs);
    } 
    catch (Exception e) {
        e.printStackTrace();
    }
    return id;
}
于 2017-08-07T06:21:57.813 回答