Error inserting data into SQLite database [closed]

1

Class that creates 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 class BankAction.java 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 that inserts data into the bank

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 giving

Please fix your application to end transactions in progress properly and to close the database when it is no longer needed.
    
asked by anonymous 04.04.2017 / 01:34

2 answers

1

I believe the connection is not yet created, so try assigning 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 closing the connection to the bank at the end of the line I entered using the command db.close();

    
04.04.2017 / 02:10
1

Translating the error:

  

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

Look, you're calling mDbHelper in the cadastrar_periodic method without instantiating it. Here's 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
}
    
04.04.2017 / 02:05