When I use the getPesoCount() like in the:
Log.v("SQL", String.valueOf(getPesoCount()));
I get aN error:
FATAL EXCEPTION: main
java.lang.IllegalStateException: attemp to re-open an already-closed object: android.database.sqlite.SQLiteQuery(mSql = SELECT * FROM pesoTable)…
public void addPeso(int peso, String date) {
SQLiteDatabase db = this.getWritableDatabase();
ContentValues values = new ContentValues();
values.put(KEY_PESO, peso); // Contact Name
values.put(KEY_DATE, date); // Contact Phone Number
// Inserting Row
db.insert(TABLE_PESO, null, values);
Log.v("SQL", String.valueOf(getPesoCount()));
db.close(); // Closing database connection
}
public int getPesoCount() {
String countQuery = "SELECT * FROM " + TABLE_PESO;
SQLiteDatabase db = this.getReadableDatabase();
Cursor cursor = db.rawQuery(countQuery, null);
cursor.close();
// return count
return cursor.getCount();
}
If I don’t use getPesoCount(), no error occurs.
Can someone help me?
Exception tells you a lot!
If you want to use the
cursorfurther do not callcursor.close();call it when you are sure that you do not need it anymore.