SQLite的连接对象泄露 - 机器人 [英] SQLite connection object leaked - Android
问题描述
我在做我的第一个Android的应用程序,我花了一些源码教程第一,教我用一个databaseHelper扩展SQLiteOpenHelper。所以我的databaseHelper确实扩展SQLiteOpenHelper。我得到了logcat的一个SQLite连接泄漏预警所以想怎样做以修复一些建议。
I am making my first android app, and I took some sqlite tutorials first, that taught me to use a databaseHelper that extends SQLiteOpenHelper. So my databaseHelper does extend SQLiteOpenHelper. I get a sqlite connection leak warning in the Logcat so would like some advice about what to do to fix that.
我得到这个错误:
02-01 21:39:50.740: W/SQLiteConnectionPool(32061): A SQLiteConnection object for database '/data/data/com.btf271.fashionassistant/databases/clothingManager' was leaked! Please fix your application to end transactions in progress properly and to close the database when it is no longer needed.
我databaseHelper函数被调用的地方发生泄漏:
My databaseHelper functions that are called where the leak occurs:
public List<Sticker> getObjectsByGenderAndCategory(String gender, String category) {
List<Sticker> objects = new ArrayList<Object>();
String selectQuery = String.format(
"SELECT * FROM %s WHERE %s = \"%s\" AND %s = \"%s\"",
TABLE_OBJECT, KEY_GENDER, gender, KEY_CATEGORY, category);
Log.e(LOG, selectQuery);
SQLiteDatabase db = this.getReadableDatabase();
Cursor c = db.rawQuery(selectQuery, null);
try{
// looping through all rows and adding to list
if (c.moveToFirst()) {
do {
Object o = createClothingItemJavaObject(c);
// adding to object list
objects.add(o);
} while (c.moveToNext());
}
}finally {
c.close();
db.close();
}
return objects;
}
我发现这里面我会尝试明天。这晚。
感谢。
推荐答案
我所做的只是实现这个回答类似的问题一>,现在,它不显示SQL连接对象泄漏错误。我不能推荐这还不够。只用了几分钟的时间来实施和工作。
All I did was implement this answer to a similar question and now it doesn't show the SQL connection object leak error. I cannot recommend this enough. It only took a few minutes to implement and worked.
下面是code:
public class DatabaseHelper extends SQLiteOpenHelper {
private static DatabaseHelper mInstance = null;
private static final String DATABASE_NAME = "database_name";
private static final String DATABASE_TABLE = "table_name";
private static final int DATABASE_VERSION = 1;
public static DatabaseHelper getInstance(Context ctx) {
// Use the application context, which will ensure that you
// don't accidentally leak an Activity's context.
// See this article for more information: http://bit.ly/6LRzfx
if (mInstance == null) {
mInstance = new DatabaseHelper(ctx.getApplicationContext());
}
return mInstance;
}
/**
* Constructor should be private to prevent direct instantiation.
* make call to static factory method "getInstance()" instead.
*/
private DatabaseHelper(Context ctx) {
super(ctx, DATABASE_NAME, null, DATABASE_VERSION);
}
}
这篇关于SQLite的连接对象泄露 - 机器人的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!