0
I can’t update the data in the database.
In the database class are the two update and search functions.
public void atualizar(MessageEB dados){
ContentValues valores = new ContentValues();
valores.put("data", dados.getData());
bd.update("dados", valores, "_id = ?", new String[]{"" + dados.getId()});
Log.i("a", dados.getData());
bd.close();
}
public MessageEB buscarConfiguracoes() {
MessageEB messageEB = new MessageEB();
String sqlSelectTodosLivros = "SELECT * FROM dados";
Cursor c = bd.rawQuery(sqlSelectTodosLivros, null);
if(c.moveToNext()) {
messageEB.setId(c.getLong(0));
messageEB.setData(c.getString(1));
Log.i("LOG", "funcao buscar");
}
Log.i("LOG", "fora do if "+(c.getString(1)));
bd.close();
return messageEB;
}
In the Fragment where you have Edittext is this code inside a:
public void onClick(View v) {
MessageEB dados = new MessageEB();
dados.setData(editar.getText().toString());
BD bd = new BD(getActivity());
bd.atualizar(dados);
}
In the die display:
txt = (TextView) view.findViewById(R.id.campoData2);
BD db = new BD(getActivity());
String s = db.buscarConfiguracoes().getData();
txt.setText(s);
When the data goes through the function update the data is changed, I saw it with a Log, but when it goes through the searchConfigurations () the data received is the same as before the change.
How can you have changed when reading back the previous value? The log generated by
Log.i("a", dados.getData());
just tell you what thedados.getData
and not if the register has been successfully amended.– ramaral
To know if an Update has updated any record check the value it returns:
int linhasAtualizadas = bd.update("dados", valores, "_id = ?", new String[]{"" + dados.getId()});
. Do the log of that value. If zero Update failed.– ramaral
Use
Integer.toString(intValor);
– ramaral
Right. I’ll do it now.
– Rodrigo Lins
I’m sure it’s zero. Look at my answer.
– ramaral
True. But when I entered this code I was wrong setId
– Rodrigo Lins
You have read the commit that is on that line. It is incomplete because I do not know where you have kept the value of the Id of the register it wishes to amend.
– ramaral
Thank you so much! Solved my problem. Thanks!
– Rodrigo Lins