Error in getWritableDatabase()

Asked

Viewed 380 times

1

I’m trying to open the database to enter values in the class CadSalario, but I’m making a mistake in dbHelper.getWritableDatabase(); and I looked on the net and I have to call a context, someone can help me?

This is the file where I create the database:

package tcc.money.financeiro;

import android.content.Context;
import android.database.sqlite.SQLiteDatabase;
import android.database.sqlite.SQLiteOpenHelper;

public class CriaDespesas extends SQLiteOpenHelper {
    public static final String DATABASE_NAME = "MONEY";
    private static final int DATABASE_VERSION = 1;

 //------------------------------------------------------------------------DESPESA-------
    public static final String TABELA = "despesa";
    public static final String DESP_ID = "id_despesa";
    public static final String DESP_NOME = "nome_desp";
    public static final String DESP_DATA_VENC = "data_venc";
    public static final String DESP_VALOR = "valor";
    public static final String DESP_QUANT_PARC = "quant_parcelas";
    public static final String DESP_DATA_PGTO = "data_pgto";
    public static final String DESP_DATA_CAD = "data_cadastro";
    public static final String DESP_VALOR_PAGO = "valor_pago";
    public static final String DESP_PAGO = "pago";


    //-----------------------------------------------------------------CATEGORIA E SUBCATEGORIAS------
    public static final String TABELACATEG = "categoria";
    public static final String DESP_ID_CATEG = "id_categ";
    public static final String DESP_ID_SUBCATEG = "id_subcateg";
    public static final String CATEG_NOME = "descricao";
    public static final String CATEG_ID = "id_categ";


    //------------------------------------------------------------------------SALARIO----------------------
    public static final String TABELASALARIO = "salario";
    public static final String SALARIO_VAZIO = "salario_vazio";
    public static final String SALARIO_DESCRICAO = "descricao";
    public static final String SALARIO_ID = "id_salario";
    public static final String SALARIO_DATA_RECEB = "data_receb";
    public static final String SALARIO_VALOR_LIQUIDO = "valor_liquido";
    public static final String SALARIO_VALOR_ATUAL = "valor_atual";

    //--------------------------------------------------------------------------USUARIO------------
    public static final String USUARIO_APELIDO = "apelido";
    public static final String USUARIO_SENHA = "senha";
    public static final String USUARIO_LOGIN = "login";

    private static final String CREATE_TABDESPESA = "create table if not exists " +
            TABELA + "( " +     DESP_ID            + " integer primary key autoincrement, " + 
                                DESP_DATA_VENC     + " text, " +
                                DESP_DATA_PGTO     + " text, " +
                                DESP_DATA_CAD      + " text, " +
                                DESP_QUANT_PARC    + " integer, " +
                                DESP_VALOR         + " decimal(8,2), " +
                                DESP_VALOR_PAGO    + " decimal(8,2), " +
                                DESP_NOME          + " text, " +
                                DESP_PAGO          + " text, " +
                                DESP_ID_CATEG      + " text, " +
                                DESP_ID_SUBCATEG   + " text);";

    private static final String CREATE_TABSALARIO = "create table if not exists " +
     TABELASALARIO + "( " +     SALARIO_VAZIO         + " text, " + 
                                SALARIO_DESCRICAO     + " text, " +
                                SALARIO_ID            + " integer primary key autoincrement, "  +
                                SALARIO_DATA_RECEB    + " text, " +
                                SALARIO_VALOR_LIQUIDO + " decimal(8,2), " +
                                SALARIO_VALOR_ATUAL   + " decimal(8,2), " +
                                USUARIO_APELIDO       + " text, " +
                                USUARIO_LOGIN + " text, " +
                                USUARIO_SENHA         + " text);";



    public CriaDespesas(Context context) {
        super(context, DATABASE_NAME, null, DATABASE_VERSION);
    }


    @Override
    public void onCreate(SQLiteDatabase database) {
        database.execSQL(CREATE_TABDESPESA);

        database.execSQL(CREATE_TABSALARIO);



    }


    @Override
    public void onUpgrade(SQLiteDatabase db, int oldVersion, int newVersion) {
        db.execSQL("DROP TABLE IF EXISTS" + TABELA);
        onCreate(db);

    }
}

This is the class in which the error occurs:

package tcc.money.financeiro;
import tcc.money.financeiro.CriaDespesas;

import com.exemplo.tccmoney.R;

import android.app.Activity;
import android.app.AlertDialog;
import android.content.ContentValues;
import android.content.Context;
import android.content.Intent;
import android.database.SQLException;
import android.database.sqlite.SQLiteDatabase;
import android.database.sqlite.SQLiteOpenHelper;
import android.os.Bundle;
import android.view.View;
import android.widget.EditText;


public class CadSalario extends Activity{
    private static final int INCLUIR = 0;
    ListDesp minhaLista2; 

    EditText descricao;
    EditText salario;
    EditText dataReceb; 
    CriaDespesas dbHelper;
    SQLiteDatabase database;

    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.layout_atividade_salario);

        minhaLista2 = new ListDesp(); 
        descricao = (EditText) findViewById(R.id.TextDescricao);
        salario = (EditText) findViewById(R.id.editTextSalario);
        dataReceb = (EditText) findViewById(R.id.editTextRecebimento);

        dbHelper.getWritableDatabase();

    }


    public void eventoBtnGravarSalario(View view){


        minhaLista2.setDescricaoSalario(descricao.getText().toString());
        minhaLista2.setValorLiquido(Float.parseFloat(salario.getText().toString()));
        minhaLista2.setDataReceb(dataReceb.getText().toString());

            ContentValues values = new ContentValues();
            values.put(CriaDespesas.SALARIO_DESCRICAO, minhaLista2.getDescricaoSalario());
            values.put(CriaDespesas.SALARIO_DATA_RECEB, minhaLista2.getDataReceb());
            values.put(CriaDespesas.SALARIO_VALOR_LIQUIDO, minhaLista2.getValorLiquido());
        database.insert(CriaDespesas.TABELA, null, values);
         }  
}
  • "Gives error" is too generic, say on which line and which error is returned.

  • What a mistake?...

2 answers

0

Try to change that line:

dbHelper.getWritableDatabase();

That’s why:

dgHelper = new CriaDespesas(this);
database = dbHelper.getWritableDatabase();

0

Your dbHelper is not initiated in its Activity. In his oncreate(..._) incise it as follows:

dgHelper = new CriaDespesas(this);

Browser other questions tagged

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