Change a value in a column in sqlite

后端 未结 2 2006
孤城傲影
孤城傲影 2021-02-19 09:52

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 Col         


        
相关标签:
2条回答
  • 2021-02-19 10:39

    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});
    
    0 讨论(0)
  • 2021-02-19 10:43

    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;
    }
    
    0 讨论(0)
提交回复
热议问题