Problem with database data update (Android)

Asked

Viewed 40 times

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 the dados.getData and not if the register has been successfully amended.

  • 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.

  • Use Integer.toString(intValor);

  • Right. I’ll do it now.

  • I’m sure it’s zero. Look at my answer.

  • True. But when I entered this code I was wrong setId

  • 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.

  • Thank you so much! Solved my problem. Thanks!

Show 3 more comments

1 answer

0


You are creating a new instance of MessageEB, gives it the value of editText and then calls the method to update the data.

However, this method requires the value of Id, value that is not "setate" in dados.
You will need to change the code in order to assign the Id of the register it wishes to amend.

public void onClick(View v) {

    MessageEB dados = new MessageEB();
    dados.setData(editar.getText().toString());

    dados.setId = //Não sei onde você o guardou

    BD bd = new BD(getActivity());
    bd.atualizar(dados);
}
  • Thanks! It worked out.

Browser other questions tagged

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