2013-05-29 13 views
0

因此,我只是最終確定了一段時間我一直在研究的應用程序上的所有內容。我在控制檯中遇到的唯一錯誤是DatabaseObjectNotClosedException。它不會導致應用程序關閉或崩潰或任何事情,但我擔心這一點,因爲我已經讀過一個未封閉的光標可能是一個大問題。DatabaseObjectNotClosedException在控制檯中

因此,這裏的錯誤消息:

05-28 20:40:01.598:E /數據庫(655):android.database.sqlite.DatabaseObjectNotClosedException:應用程序沒有關閉,這是光標或數據庫對象在這裏開幕

這裏是它指的是代碼:

datasource = new ProgressDataSource(this); 
    datasource.open(); 

下面是ProgressDataSource代碼:

package com.gauv.myapp; 

import java.util.Arrays; 
import java.util.Collections; 
import android.content.Context; 
import android.database.Cursor; 
import android.database.SQLException; 
import android.database.sqlite.SQLiteDatabase; 
import com.jjoe64.graphview.GraphView.GraphViewData; 

public class ProgressDataSource { 

    private SQLiteDatabase database; 
    private MySQLiteHelper dbHelper; 

    public ProgressDataSource(Context context) { 
     dbHelper = new MySQLiteHelper(context); 
    } 

    public void open() throws SQLException { 
     database = dbHelper.getWritableDatabase(); 
    } 

    public void close() { 
     dbHelper.close(); 
    } 

    public GraphViewData[] fetchProgress(long dayExerciseDataID) {  
     Cursor cursor = database.rawQuery("select " + MySQLiteHelper.COLUMN_1RM + " " + 
       "from " + MySQLiteHelper.TABLE_LOGS + " " + 
       "where " + MySQLiteHelper.COLUMN_ID_DAY_EXERCISE + "='" + dayExerciseDataID + "' " + 
       "order by " + MySQLiteHelper.COLUMN_DATE + " desc limit 30", null); 

     GraphViewData[] data = new GraphViewData[cursor.getCount()]; 
     Long[] onerms = new Long[cursor.getCount()]; 

     int i = 0; 

     cursor.moveToFirst(); 
     while (cursor.isAfterLast() == false) 
     { 
      onerms[i] = cursor.getLong(0); 
      i++; 
      cursor.moveToNext(); 
     } 

     Collections.reverse(Arrays.asList(onerms)); 

     int y = 1; 

     for (int x = 0; x < onerms.length; x++) { 
      data[x] = new GraphViewData(y, onerms[x]); 
      y++; 
     } 

     return data; 
    } 

    public String fetchProgressCount(long dayExerciseDataID) { 
     Cursor cursor = database.rawQuery("select " + MySQLiteHelper.COLUMN_1RM + " " + 
       "from " + MySQLiteHelper.TABLE_LOGS + " " + 
       "where " + MySQLiteHelper.COLUMN_ID_DAY_EXERCISE + "='" + dayExerciseDataID + "' " + 
       "order by " + MySQLiteHelper.COLUMN_DATE + " desc limit 12", null); 

     return String.valueOf(cursor.getCount()); 
    } 
} 

我是否只需要將cursor.close()添加到fetchProgress函數?

回答

2

您需要關閉您的數據庫,你用它做之後,因此,所有你需要做的就是調用datasource.close()是的,你也應該關閉所有遊標您不再使用從內存

+0

我只是將它添加到的onPause功能在我的活動,它解決了問題。 – scarhand

0

此錯誤釋放表示您打開了一個數據庫連接。你的代碼片段證實了這一點。一個好的做法是從光標獲取信息到某個域對象,然後清理光標和連接。

代碼示例:

// open db connection 
DataBaseHelper db = new DataBaseHelper(this); 
db.open(); 

// retrieve your items using a cursor object 
Cursor cursor = db.rawQuery("SELECT * FROM SOME_DB", null); 

// iterate all your items here and move to some collection or domain model 
// ... 

// and clean up your mess 
cursor.close(); 
db.close();