Data update Sqlite Android

Asked

Viewed 2,723 times

2

How to take the information that is on the screen and do an Update in the database.

Code displaying information in Edittext.

public class DatabasesDAO extends SQLiteOpenHelper {

private static final String DATABASE = "baseteste";
private static final int VERSAO = 1;
private static final String TAG = "Teste";

private SQLiteDatabase db;

private ConfiguracoesSistema teste;

public DatabasesDAO(Context context) {
    super(context, DATABASE, null, VERSAO);
}


public Configuracao buscarConfiguracoes() {
    // Variável para utilizar a query no Cursor
    String sql = "select * from dj_tb_sis";
    // Objeto que recebe os registros do Banco de Dados
    Cursor cursor = getReadableDatabase().rawQuery(sql, null);
    // Instância do objeto que será retornado
    Configuracao configuracao = new Configuracao();

    // Carrega os atributos do Banco de Dados
    if (cursor.moveToNext()) {
        configuracao.setCodigoSistema(cursor.getLong(0));
        configuracao.setNomeEmpresa(cursor.getString(1));
        configuracao.setEnderecoEmpresa(cursor.getString(2));
        configuracao.setCidade(cursor.getString(3));
        configuracao.setTelefoneEmpresa(cursor.getString(4));
        configuracao.setCodigoEmpresa(cursor.getString(5));
        configuracao.setNumeroIP(cursor.getString(6));
        configuracao.setUsuarioFTP(cursor.getString(7));
        configuracao.setSenhaFTP(cursor.getString(8));
        configuracao.setPortaFTP(cursor.getString(9));
        configuracao.setCaminhoImportFTP(cursor.getString(10));
        configuracao.setCaminhoExportFTP(cursor.getString(11));
    }

    cursor.close();

    return configuracao;
}

In case the user changes some field wanted it to update already in the database.

My Activity code that displays the given fields and update button.

@Override
protected void onCreate(Bundle savedInstanceState) {
    super.onCreate(savedInstanceState);
    setContentView(R.layout.tela_configuracoes);

    razaoSocial = (EditText) findViewById(R.id.edtRazaoSocial);
    enderecoEmpresa = (EditText) findViewById(R.id.edtEndereco);
    cidade = (EditText) findViewById(R.id.edtCidade);
    telefoneEmpresa = (EditText) findViewById(R.id.edtTelefone);
    codigoEmpresa = (EditText) findViewById(R.id.edtQualEmpresa);
    numeroIP = (EditText) findViewById(R.id.edtEnderecoIP);
    usuarioFTP = (EditText) findViewById(R.id.edtUsuarioFTP);
    senhaFTP = (EditText) findViewById(R.id.edtSenhaFTP);
    portaFTP = (EditText) findViewById(R.id.edtPortaFTP);
    caminhoImportFTP = (EditText) findViewById(R.id.edtCaminhoImportar);
    caminhoExportFTP = (EditText) findViewById(R.id.edtCaminhoExportar);

    dao = new DatabasesDAO(this);

    Configuracao configuracao = dao.buscarConfiguracoes();
    razaoSocial.setText(configuracao.getNomeEmpresa().toString());
    enderecoEmpresa.setText(configuracao.getEnderecoEmpresa().toString());
    cidade.setText(configuracao.getCidade().toString());

    telefoneEmpresa.setText(configuracao.getTelefoneEmpresa().toString());
    codigoEmpresa.setText(configuracao.getCodigoEmpresa().toString());
    numeroIP.setText(configuracao.getNumeroIP().toString());
    usuarioFTP.setText(configuracao.getUsuarioFTP().toString());
    senhaFTP.setText(configuracao.getSenhaFTP().toString());
    portaFTP.setText(configuracao.getPortaFTP().toString());
    caminhoImportFTP.setText(configuracao.getCaminhoImportFTP().toString());
    caminhoExportFTP.setText(configuracao.getCaminhoExportFTP().toString());

    // Button btnAtualiza = (Button) findViewById(R.id.btnAtualizarDados);
}

Here’s what I tried to build to do Update.

public int update(Configuracao config) throws Exception {
        SQLiteDatabase sqlLite = new DatabasesDAO(teste).getWritableDatabase();

        ContentValues content = new ContentValues();

                content.put("t_nom_emp", config.getNomeEmpresa());
                content.put("t_end_emp", config.getEnderecoEmpresa());

                String where = "u_cod_sis = ?";

                String argumentos[] = { config.getNomeEmpresa() };

        return sqlLite.update("dj_tb_sis", content, where, argumentos);
    }
  • You want to update when you click on a right button? Then you have to fetch the values in the view, because they are not automatically updated in your "settings" variable.

  • Would you have any hint of how I start this method to be able to search for the information and assign pro update? So I saw I have to have a Where for the ID. Only I got all tangled up here.

1 answer

2


The way to fetch the Edittext variable inside the update event, for example:

Button btnAtualiza;

@Override
protected void onCreate(Bundle savedInstanceState) {

   ...
   btnAtualiza = (Button) findViewById(R.id.btnAtualizarDados);
}

@Override
public void onClick(View v) {

    if (v == btnAtualiza) { //o teu botão de atualizar o formulário

         String razaoS = razaoSocial.getText().toString();

         ...
         update(/*variáveis*/);
    }        
}
  • Why this Validate? I understood how to get the value from inside Edittext only did not understand validate.

  • if you have more than one button, example validate and cancel, it checks if it is the validate, I edited see the example.

  • I still don’t understand that. What I want is that in the Edittext fields that have information, if it adds any letter, I want that when you click the Update button, it picks up and updates the line in the database..

  • I’ll edit and for your example, see.

  • Just look at my Activity http://pastebin.com/embed_iframe.php?i=Fiapq1wc BD http://pastebin.com/embed_iframe.php?i=qWCgWuEM This is causing me the following error. http://pastebin.com/embed_iframe.php?i=ET2AWj2V

  • You have to initialize the cfg variable, Configuracao cfg = new Configuracao();. I don’t know how you got the builder, but that’s the way it is.

  • It worked. Only it seems to me he’s saving a new datum and not changing it. Because when I restart Activity it returns to me the initial value without change.

  • This may already have something to do with the update. Select * to see what you have saved after the update.

  • Jorge B. It worked, it was my inattention. I initialized the Settings twice and I wasn’t calling the Search. cfg = dao.searchConfigurations();

  • 1

    It all worked out now. Thank you very much.

Show 5 more comments

Browser other questions tagged

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