Update all table records with SQLiteDatabase.update ()

1

I have the following code to update a field in my table. I would like to know if I would like to change the "note" and "missing" fields in all the records in the table.

Example: put all records with note = 8 and missing = 5

public void Update_Exercicios(String id,int dados1,int dados2) {
    ContentValues valores= new ContentValues();
    valores.put("nota",dados1);
    valores.put("falta",dados2);
    banco.update("CriarTreinoExercicio", valores,"ID=?",new String[] {id});
}
    
asked by anonymous 31.03.2016 / 19:33

2 answers

1

If you want to change all records then you should not define the WHERE clause.

The method update () has 4 parameters from left to right are:

1 - String: Name of the table to change. 2 - ContentValues: Map with the field names to change and their respective new values. 3 - String: The WHERE clause to apply during update . 4 - String []: Arguments to pass to WHERE clause

When calling method update() :

banco.update("CriarTreinoExercicio", valores,"ID=?",new String[] {id});  

is being passed in the 3rd parameter, which indicates the WHERE clause, a value that will be translated as WHERE ID = id , with only the registry having that id changed.

To change all records, you must pass null in the 3rd and 4th parameters:

banco.update("CriarTreinoExercicio", valores, null, null);
    
31.03.2016 / 19:54
0

Try changing the line:

banco.update("CriarTreinoExercicio", valores,"ID=?",new String[] {id});

To:

 banco.update("CriarTreinoExercicio", valores,"ID=ID",new String[] {id});

Or to:

banco.update("CriarTreinoExercicio", valores,null,null);

Or to:

banco.update("CriarTreinoExercicio", valores);

The last option is the most correct, but I do not remember if it is possible, so try in the bottom order.

    
31.03.2016 / 19:39