2017-05-01 42 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 

现在你可以关闭游标

相关问题