2017-02-20 33 views
1

從兩天我試圖解決我的問題我會提到每個解決方案在這裏討論,並做了所有的可能性,仍然繼續顯示Sql連接泄漏。由於這種應用是不穩定的下面是我的dBhelper code.It是一個測驗應用Sqlite連接儘管一切都關閉在android工作室泄漏

private static String DB_NAME = "NavyDb6.db"; 
private static String DB_PATH = ""; 
private SQLiteDatabase mDataBase; 
private Context mContext = null; 

public DbHelper(Context context) { 
    super(context, DB_NAME, null, 1); 


    DB_PATH = context.getApplicationInfo().dataDir + "/databases/"; 
    File file = new File(DB_PATH + "NavyDb6.db"); 
    if (file.exists()) 
     openDataBase(); // Add this line to fix db.insert can't insert values 

    this.mContext = context; 
} 


public void openDataBase() { 
    String myPath = DB_PATH + DB_NAME; 
    mDataBase = SQLiteDatabase.openDatabase(myPath, null, SQLiteDatabase.OPEN_READWRITE); 
} 

public void copyDataBase() throws IOException { 
    try { 
     InputStream myInput = mContext.getAssets().open(DB_NAME); 
     String outputFileName = DB_PATH + DB_NAME; 
     OutputStream myOutput = new FileOutputStream(outputFileName); 

     byte[] buffer = new byte[1024]; 
     int length; 
     while ((length = myInput.read(buffer)) > 0) 
      myOutput.write(buffer, 0, length); 

     myOutput.flush(); 
     myOutput.close(); 
     myInput.close(); 
    } catch (Exception e) { 
     e.printStackTrace(); 
    } 
} 

private boolean checkDataBase() { 
    SQLiteDatabase tempDB = null; 
    try { 
     String myPath = DB_PATH + DB_NAME; 
     tempDB = SQLiteDatabase.openDatabase(myPath, null, SQLiteDatabase.OPEN_READWRITE); 
    } catch (SQLiteException e) { 
     e.printStackTrace(); 
    } 
    if (tempDB != null) 
     tempDB.close(); 
    return tempDB != null ? true : false; 
} 

public void createDataBase() throws IOException { 
    boolean isDBExists = checkDataBase(); 
    if (isDBExists) { 

    } else { 
     this.getReadableDatabase(); 
     try { 
      copyDataBase(); 
     } catch (IOException e) { 
      e.printStackTrace(); 
     } 
    } 


} 




@Override 
public void onCreate(SQLiteDatabase db) {} 

@Override 
public void onUpgrade(SQLiteDatabase db, int oldVersion, int newVersion) {} 


//We need improve this function to optimize process from Playing 
public List<Question> getQuestionMode(String mode) { 
    List<Question> listQuestion = new ArrayList<>(); 
    SQLiteDatabase db = this.getWritableDatabase(); 
    Cursor c; 
    int limit = 0; 
    if (mode.equals(Common.MODE.EASY.toString())) 
     limit = 20; 
    else if (mode.equals(Common.MODE.MEDIUM.toString())) 
     limit = 20; 
    else if (mode.equals(Common.MODE.HARD.toString())) 
     limit = 30; 
    else if (mode.equals(Common.MODE.HARDEST.toString())) 
     limit = 30; 

    try { 
     c = db.rawQuery(String.format("SELECT * FROM Question ORDER BY Random() LIMIT %d", limit), null); 
     if (c == null) return null; 
     c.moveToFirst(); 
     do { 

      int Id = c.getInt(c.getColumnIndex("ID")); 
      //int cid= c.getInt(c.getColumnIndex("C_ID")); 
      String Qus = c.getString(c.getColumnIndex("Qus")); 
      String AnswerA = c.getString(c.getColumnIndex("AnswerA")); 
      String AnswerB = c.getString(c.getColumnIndex("AnswerB")); 
      String AnswerC = c.getString(c.getColumnIndex("AnswerC")); 
      String AnswerD = c.getString(c.getColumnIndex("AnswerD")); 
      String CorrectAnswer = c.getString(c.getColumnIndex("CorrectAnswer")); 

      Question question = new Question(Id, Qus, AnswerA, AnswerB, AnswerC, AnswerD, CorrectAnswer); 
      listQuestion.add(question); 
      db.close(); 
     } 
     while (c.moveToNext()); 
     c.close(); 

    } catch (Exception e) { 
     e.printStackTrace(); 
    } 
    this.close(); 
    return listQuestion; 
} 

更新分數

你做錯了在 getQuestionMode方法
public void insertScore(int score) { 
    String query = "INSERT INTO Rank(Score) VALUES(" + score + ")"; 
    mDataBase.execSQL(query); 
} 


public List<Rank> getRanking() { 
    List<Rank> listRanking = new ArrayList<>(); 
    SQLiteDatabase db = this.getReadableDatabase(); 
    Cursor c = db.rawQuery("SELECT * FROM Rank Order By Score DESC LIMIT 5;", null); 
    try { 

     if (c == null) return null; 
     c.moveToNext(); 
     do { 
      int Id = c.getInt(c.getColumnIndex("ID")); 
      int Score = c.getInt(c.getColumnIndex("Score")); 


      Rank ranking = new Rank(Id, Score); 
      listRanking.add(ranking); 
     } while (c.moveToNext()); 

    } catch (Exception e) { 
     e.printStackTrace(); 
    } 
    finally { 
     c.close(); 
     db.close(); 
    } 
    return listRanking; 

} 

回答

0

有許多東西。是的那些:

  • 閉讀取第一項
  • 閉光標後的分貝連接外finally塊

另外,我想避免這種線if (c == null) return null;

我會推薦使用singleton pattern來訪問數據。

要解決當前的問題,試試這個:

public List<Question> getQuestionMode(String mode) { 
    List<Question> listQuestion = new ArrayList<>(); 
    SQLiteDatabase db = this.getWritableDatabase(); 
    Cursor c = null; 
    int limit = 0; 
    if (mode.equals(Common.MODE.EASY.toString())) 
     limit = 20; 
    else if (mode.equals(Common.MODE.MEDIUM.toString())) 
     limit = 20; 
    else if (mode.equals(Common.MODE.HARD.toString())) 
     limit = 30; 
    else if (mode.equals(Common.MODE.HARDEST.toString())) 
     limit = 30; 

    try { 
     c = db.rawQuery(String.format("SELECT * FROM Question ORDER BY Random() LIMIT %d", limit), null); 
     if(c.moveToFirst()) { 
      do { 
       int Id = c.getInt(c.getColumnIndex("ID")); 
       //int cid= c.getInt(c.getColumnIndex("C_ID")); 
       String Qus = c.getString(c.getColumnIndex("Qus")); 
       String AnswerA = c.getString(c.getColumnIndex("AnswerA")); 
       String AnswerB = c.getString(c.getColumnIndex("AnswerB")); 
       String AnswerC = c.getString(c.getColumnIndex("AnswerC")); 
       String AnswerD = c.getString(c.getColumnIndex("AnswerD")); 
       String CorrectAnswer = c.getString(c.getColumnIndex("CorrectAnswer")); 

       Question question = new Question(Id, Qus, AnswerA, AnswerB, AnswerC, AnswerD, CorrectAnswer); 
       listQuestion.add(question); 
       } 
       while (c.moveToNext()); 
     } 

    } catch (Exception e) { 
     e.printStackTrace(); 
    } finally { 
     if(null != c) 
      c.close(); 
     if(null != db) 
      db.close(); 
    } 

    return listQuestion; 
} 

更新:

你仍然有openDataBase()方法打開一個數據庫。最後加上checkDataBase()

你可以嘗試重寫你的幫手this(看例子!)。

+0

欣賞你的時間但我仍然沒有工作同樣的警告不斷顯示 –

+0

請給我你的錯誤日誌。 – Goran

+0

2265-2274/com.example.kdeepti.navyquiz D/dalvikvm:GC_FOR_ALLOC釋放4K,50%空閒26199K/51616K,暫停20ms,總共20ms 2265-2274/com.example.kdeepti.navyquiz W/SQLiteConnectionPool:SQLiteConnection數據庫'xx'的對象被泄露!請修復您的應用程序以正確結束正在進行的事務,並在不再需要時關閉數據庫。 2038-2038/com.google.android.gms D/dalvikvm:GC_FOR_ALLOC已釋放699K,25%免費4863K/6424K,暫停9ms,共9ms –