Update all table records with Sqlitedatabase.update()

Asked

Viewed 90 times

1

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

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

2 answers

1


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

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

1 - String: Name of table to change.
2 - Contentvalues: Map with the names of the fields to be changed and their new values.
3 - String: The clause WHERE to be applied during the update.
4 - String[]: Arguments to be addressed WHERE

In the call to the method update():

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

is being passed in the third parameter, the one indicating the clause WHERE, a value that will be translated as WHERE ID = id, only the register containing that id.

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

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

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 order from bottom to top.

Browser other questions tagged

You are not signed in. Login or sign up in order to post.