2017-05-01 43 views
1

我目前正在研究一個大學項目,這是一個飛鏢計算器應用程序,我不斷收到數據庫泄漏錯誤,即使我認爲我在使用它們後關閉了所有數據庫和遊標。我還得到一個「W/IdleConnectionHandler:刪除一個從未存在的連接!」程序開始時出現錯誤。我的應用程序不斷崩潰,SQLite數據庫泄漏錯誤

我logcat的有以下錯誤:

05-01 08:33:47.801 9504-9581/? W/IdleConnectionHandler: Removing a connection that never existed! 
05-01 08:43:46.141 9504-9581/? W/IdleConnectionHandler: Removing a connection that never existed! 

和:

05-01 09:08:06.841 4858-4866/? W/SQLiteConnectionPool: A SQLiteConnection object for database '+data+user+0+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. 
05-01 09:08:06.841 4858-4866/? W/SQLiteConnectionPool: A SQLiteConnection object for database '+data+user+0+com_google_android_gms+databases+help_responses_db_18' was leaked! Please fix your application to end transactions in progress properly and to close the database when it is no longer needed. 
05-01 09:08:06.841 4858-4866/? W/SQLiteConnectionPool: A SQLiteConnection object for database '+data+user+0+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. 

我的數據庫輔助類看起來是這樣的:

package com.dartsapp.niall.darts_calculator_cs2048; 

import android.content.Context; 
import android.database.Cursor; 
import android.database.sqlite.SQLiteDatabase; 
import android.database.sqlite.SQLiteOpenHelper; 

public class DatabaseHelper extends SQLiteOpenHelper { 

private static final String database_name = "finishes.db"; 
private static final String table_name = "finishes_table"; 
//private static final String column1 = "score"; 
//private static final String column2 = "finish"; 
private static DatabaseHelper sInstance = null; 

public static DatabaseHelper getInstance(Context context) { 

    if (sInstance == null) { 
     sInstance = new DatabaseHelper(context.getApplicationContext()); 
    } 
    return sInstance; 
} 


private DatabaseHelper(Context context) { 
    super(context, database_name, null, 1); 
    //SQLiteDatabase db = this.getWritableDatabase(); 
    //db.close(); 
} 

public Cursor getData(int playerScore) { 
    SQLiteDatabase db = this.getWritableDatabase(); 
    Cursor res = db.rawQuery("select FINISH from "+table_name+" where SCORE = "+ playerScore,null); 
    res.close(); 
    db.close(); 
    return res; 
} 

@Override 
public void onCreate(SQLiteDatabase db) { 
    db.execSQL("CREATE TABLE IF NOT EXISTS "+table_name+ "(SCORE INTEGER, FINISH INTEGER)"); 
    String finish2 = 
      "INSERT or replace INTO tbl_Contain (SCORE, FINISH) VALUES('2','D1')" ; 
    db.execSQL(finish2); 

// LONG list of INSERTs - OMITTED 

    String finish170 = 
      "INSERT or replace INTO tbl_Contain (SCORE, FINISH) VALUES('170', 'T20 T20 BULL')" ; 
    db.execSQL(finish170); 
    db.close(); 
} 

@Override 
public void onUpgrade(SQLiteDatabase db, int oldVersion, int newVersion) { 
    db.execSQL("DROP TABLE IF EXISTS " + table_name); 
    onCreate(db); 
    db.close(); 
} 

} 

我受到嚴重的時間壓力位置,因此任何幫助將不勝感激!`

+0

如果你的意思是它返回給調用者不能關閉光標。讓調用者在消費後關閉它。 –

+1

爲什麼你在onCreate上執行你的查詢 –

+0

因爲我希望數據一旦創建就可以放入數據庫,但我對android studio沒有經驗,所以我不確定這是否正確。有沒有更好的方法來插入數據? – nialln1

回答

0

您在獲取記錄之前關閉了光標。

你需要添加到您的代碼之前關閉遊標

res.moveToFirst(); //move Cursor to first record 
while (cur.isAfterLast() == false) { // while Cursor not point to last record 
Log.v("YOURTAG", cur.getString("yourString")); // get your data 
//ToDo save your data 
cur.moveToNext(); 
} 
// ToDo return your data 

現在你可以關閉遊標

相關問題