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.
– Jorge B.
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.
– user2362558