Android 更改sqlite中列中的值

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/11563732/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me): StackOverFlow

提示:将鼠标放在中文语句上可以显示对应的英文。显示中英文
时间:2020-08-20 08:03:28  来源:igfitidea点击:

Change a value in a column in sqlite

androidsqlite

提问by Gabrielle

I need to update a value in a column from a certain table. I tried this :

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

 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();
                 }

        }

and I call this method like this :

我这样称呼这个方法:

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

which means that I want to set the value 1in the column sys_roadmap_status_mobile_idwhen id_roadmap = id.

这意味着我要设置的值1sys_roadmap_status_mobile_idid_roadmap = id.

The problem is that nothing happens. Where is my mistake?

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

回答by WarrenFaith

Easy solution:

简单的解决方案:

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

Better solution:

更好的解决方案:

ContentValues cv = new ContentValues();
cv.put(ColumnName, newValue);
db.update(TABLE_NAME, cv, Column + "= ?", new String[] {rowId});

回答by BAIJU SHARMA

The below solution works for me for updating single row values:

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

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;
}