Error entering data in Sqlite database

Asked

Viewed 507 times

1

Class creating the bank

public class CriaBanco extends SQLiteOpenHelper {

private static final String NOME_BANCO = "lista.db";
private static final int VERSAO_BANCO = 1;

public CriaBanco(Context context) {
    super(context, NOME_BANCO, null, VERSAO_BANCO);
}

@Override
public void onCreate(SQLiteDatabase db) {

    final String SQL_CRIAR_TABELA = "CREATE TABLE lista_notify (" +
            "id INTEGER PRIMARY KEY AUTOINCREMENT "+
            "hora_notificacao TEXT NOT NULL"+
            "texto_notificacao TEXT NOT NULL)";

    db.execSQL(SQL_CRIAR_TABELA);
}

@Override
public void onUpgrade(SQLiteDatabase db, int oldVersion, int newVersion) {

}
}

Mainactivity.java where the instance of the Criabanco.java class is created.

public class MainActivity extends AppCompatActivity {

private EditText ed_hora_notificacao;
private EditText ed_texto_notificacao;
private CriaBanco mDbHelper;

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

    CriaBanco banco = new CriaBanco(this);

}}

Method of entering data into the database

public void cadastrar_periodic(View v){
    SQLiteDatabase db = mDbHelper.getWritableDatabase();

    ContentValues values = new ContentValues();
    values.put("hora_notificacao", "Toto");
    values.put("texto_notificacao", "Terrier");

    long newRowId = db.insert("lista_notify", null, values);
}

Error text that gives

Please fix your application to end transactions in progress properly and to close the database when it is no longer needed.
  • 1

    Already tried after that insert calling db.close();

  • yes, but the mistake continues.

2 answers

1

Translating the error:

Please fix your application to finalize ongoing transactions and close the database when it is no longer needed.

Look at you, you’re calling the mDbHelper in the method cadastrar_periodic without instantiating it. See below how your method should look:

public void cadastrar_periodic(View v){
    mDbHelper = new CriaBanco(this); // criando uma instancia 

    SQLiteDatabase db = mDbHelper.getWritableDatabase();

    ContentValues values = new ContentValues();
    values.put("hora_notificacao", "Toto");
    values.put("texto_notificacao", "Terrier");

    long newRowId = db.insert("lista_notify", null, values);

    db.close(); // fechando o banco de dados
}
  • @Demetrius see if it solves your problem like this?!

1

I believe the connection is not yet created try to assign the new Cribanco to mHelper as below:

public class MainActivity extends AppCompatActivity {

private EditText ed_hora_notificacao;
private EditText ed_texto_notificacao;
private CriaBanco mDbHelper;

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

     this.mDbHelper = new CriaBanco(this);

}}

something else try to close the connection to the bank at the end of the line I entered using the command db.close();

Browser other questions tagged

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