当前位置: 动力学知识库 > 问答 > 编程问答 >

android - re-open already closed

问题描述:

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?

网友答案:
attempt to re-open an already-closed object:

Exception tells you a lot!

If you want to use the cursor further do not call cursor.close(); call it when you are sure that you do not need it anymore.

网友答案:
 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)));
        db.close(); // Closing database connection

    }




    public int getPesoCount(db) {
        String countQuery = "SELECT  * FROM " + TABLE_PESO;
        Cursor cursor = db.rawQuery(countQuery, null);
        int count = cursor.getCount();
        cursor.close();
        return count;
    }
分享给朋友:
您可能感兴趣的文章:
随机阅读: