Problem with database data visualization

Asked

Viewed 49 times

2

In each code I explain the problem.

public void onCreate(SQLiteDatabase bd) {
    bd.execSQL("create table tabela(_id integer primary key autoincrement, data text not null, orcamento text not null);");
}

My bank has a table and the id, date and budget fields.

public void inserir(MessageEB tabela){
    ContentValues valores = new ContentValues();
    valores.put("data", tabela.getData());
    valores.put("orcamento", tabela.getOrcamento());

    bd.insert("tabela", null, valores);
}

If you have no data saved the program triggers this code. A data must already exist in the bank and the user should only update it. This data must already exist for the user, so if the program is started for the first time, as it will have nothing in the bank, it triggers the insert function.

To update the data:

public void atualizar(MessageEB tabela){
    ContentValues valores = new ContentValues();
    valores.put("data", tabela.getData());
    valores.put("orcamento", tabela.getOrcamento());

    bd.update("dados", valores, "_id = ?", new String[]{"" + tabela.getId()});
    int linhasAtualizadas = bd.update("dados", valores, "_id = ?", new String[]{"" + tabela.getId()});
    Log.i("LOG", Integer.toString(linhasAtualizadas));

    bd.close();
}

If I take the bd.close(); the project works, but it gives a certain error because the function does not close the bank.

User will update data separately.

Looking for a datum:

public MessageEB buscarConfiguracoes() {
    MessageEB messageEB = new MessageEB();

    String[] colunas = new String[]{"_id","data", "orcamento"};
    Cursor c = bd.query("tabela", colunas, null, null, null, null, null);

    if(c.getCount() > 0) {
        c.moveToFirst();

        messageEB.setId(c.getLong(0));
        messageEB.setData(c.getString(1));
        messageEB.setOrcamento(c.getString(2));

        Log.i("LOG", "funcao buscar"); //teste
    }

    bd.close();
    return messageEB;
}

In the FragmentInicio where the value of the data will be shown has this condition to know if you already have something saved or not:

MessageEB dados = new MessageEB();
    BD db = new BD(getActivity());
    String data = db.buscarConfiguracoes().getData();

    if(data == null) {
       dados.setData("DD/MM/AAAA");
        dados.setOrcamento("0,00");
        db.inserir(dados);
        txt.setText("DD/MM/AAAA");
        txtOrcamento.setText("0,00");
    } else {
        txt.setText(data);
        String orcamento = db.buscarConfiguracoes().getOrcamento();
        txtOrcamento.setText(orcamento);
    }

But when entering the data gives error:

android.database.sqlite.SQLiteStatement.releaseAndUnlock(SQLiteStatement.java:283)
        at android.database.sqlite.SQLiteStatement.executeInsert(SQLiteStatement.java:116)
        at android.database.sqlite.SQLiteDatabase.insertWithOnConflict(SQLiteDatabase.java:1732)
        at android.database.sqlite.SQLiteDatabase.insert(SQLiteDatabase.java:1605)
        at br.com.aplicacao.teste.BancoDeDados.BD.inserir(BD.java:25)
        at br.com.aplicacao.teste.fragment.FragmentInicio.onCreateView(FragmentInicio.java:35)
        at android.support.v4.app.Fragment.performCreateView(Fragment.java:1786)

In the Fragment where have the EditTexts I want to recover the value:

BD db = new BD(getActivity());
    String data = db.buscarConfiguracoes().getData();
    editar.setText(data);
    String orcamento = db.buscarConfiguracoes().getOrcamento();
    editarGasto.setText(orcamento);

But when it comes to string orcamento, makes a mistake.

  • Saying it’s wrong isn’t enough.

  • In order for the user to update a field in a project, there must already be something in the database. Therefore, I need to insert something when I have nothing saved because the program will start with nothing and your bank. This is the first problem. The second problem is that I am not able to recover the data from this database to insert into Edittexts fields. That which I wrote is in the codes I put in the question.

1 answer

1


It seems to me that you’re trying to access the bank when it’s closed.

Maybe putting a db.open() within the insert method resolve. Do not forget the bd.close() also.

  • Yes, it’s true. I was trying to access it with it closed. I already managed to solve all the problems. This and the others. Thank you for your attention.

Browser other questions tagged

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