Error in database, Android Studio

2

I have a method in the DBHelper class (which is where the database is created), and what I want to do in this method is read data from one table and add it to another table but it gives me the following error:

01-20 23:51:07.697 5880-5888/? W/SQLiteConnectionPool: A SQLiteConnection      object for database '/data/data/com.google.android.gms/databases/metrics.db' was leaked!  Please fix your application to end transactions in progress properly and to close the database when it is no longer needed.
01-20 23:51:07.697 5880-5888/? W/SQLiteConnectionPool: A SQLiteConnection object for database '/data/data/com.google.android.gms/databases/help_responses.db' was leaked!  Please fix your application to end transactions in progress properly and to close the database when it is no longer needed.
01-20 23:51:07.697 5880-5888/? W/SQLiteConnectionPool: A SQLiteConnection object for database '/data/data/com.google.android.gms/databases/auto_complete_suggestions.db' was leaked!  Please fix your application to end transactions in progress properly and to close the database when it is no longer needed.

The method I use is the following:

public void añadirErrores(int[] pregMalas) {
    int preg;
    SQLiteDatabase db = getReadableDatabase();
    for (int i = 0; i < pregMalas.length; i++) {
        preg = pregMalas[i];
        Cursor c = db.rawQuery("SELECT * FROM preguntas where id = " + preg, null);
        if (c.moveToFirst()) {
            do {
                SQLiteDatabase dbw = getWritableDatabase();
                ContentValues values = new ContentValues();
                values.put("id", c.getInt(0));
                values.put("pregunta", c.getString(1));
                values.put("respuesta_correcta", c.getString(2));
                values.put("respuesta_falsa_1", c.getString(3));
                values.put("respuesta_falsa_2", c.getString(4));
                values.put("recurso", c.getString(5));
                dbw.insert("testErrores", null, values);
                dbw.close();
            } while (c.moveToNext());
        }
    }
    db.close();
}

I've tried putting the db.close() one and two brackets higher but still giving me an error.

    
asked by FranEET 20.01.2017 в 23:58
source

1 answer

2

You are closing the instance of the Database when you are performing an operation on it, you must close the instance when you do not occupy it, this also applies to the cursor.

public void añadirErrores(int[] pregMalas) {
    int preg;
    SQLiteDatabase db = getReadableDatabase();
    SQLiteDatabase dbw = getWritableDatabase();
    for (int i = 0; i < pregMalas.length; i++) {
        preg = pregMalas[i];
        Cursor c = db.rawQuery("SELECT * FROM preguntas where id = " + preg, null);
        if (c.moveToFirst()) {
            do {

                ContentValues values = new ContentValues();
                values.put("id", c.getInt(0));
                values.put("pregunta", c.getString(1));
                values.put("respuesta_correcta", c.getString(2));
                values.put("respuesta_falsa_1", c.getString(3));
                values.put("respuesta_falsa_2", c.getString(4));
                values.put("recurso", c.getString(5));
                dbw.insert("testErrores", null, values);
                //dbw.close();
            } while (c.moveToNext());
        }
          //Cierras cursor
          c.close();
    }
       //Cierras BD.
        dbw.close();
        db.close();
}
    
answered by 21.01.2017 / 00:13
source