Problem with bank data view

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 do not have any data saved, the program activates this code. A data must already exist in the database and the user should only update it. This data should already exist for the user, so if the program is started for the first time, since it will not have anything in the database, 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 out bd.close(); the project works, but it gives a certain error because the function does not close the database.

The user will update the data separately.

Finding a die:

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%% of where it will be shown the value of the data has this condition to know if it already has something recorded 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 at the time of entering the data it 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 where it has FragmentInicio I want to retrieve 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 arrives in string EditTexts , it gives error.

    
asked by anonymous 27.04.2015 / 20:16

1 answer

1

It looks like you're trying to access the bank when it's closed.

Maybe putting a db.open() inside the insert method will solve. Do not forget bd.close() too.

    
27.04.2015 / 22:04