我发现了很多类似close the connection
and的东西close the cursor
,但我做所有这些东西。SQLite 连接仍然泄漏,我收到如下警告:
A SQLiteConnection object for database was leaked!
我有一个数据库管理器,我使用以下代码在我的活动中调用它:
DatabaseManager dbm = new DatabaseManager(this);
我的数据库管理器类的代码现在如下:
public class DatabaseManager {
private static final int DATABASE_VERSION = 9;
private static final String DATABASE_NAME = "MyApp";
private Context context = null;
private DatabaseHelper dbHelper = null;
private SQLiteDatabase db = null;
public static class DatabaseHelper extends SQLiteOpenHelper {
public DatabaseHelper(Context context) {
super(context, DATABASE_NAME, null, DATABASE_VERSION);
}
@Override
public void onCreate(SQLiteDatabase db) {
//create database tables
}
@Override
public void onUpgrade(SQLiteDatabase db, int oldVersion, int newVersion) {
//destroy and recreate them
}
}
public DatabaseManager(Context ctx) {
this.context = ctx;
}
private DatabaseManager open() throws SQLException {
dbHelper = new DatabaseHelper(context);
db = dbHelper.getWritableDatabase();
if (!db.isReadOnly()) {
db.execSQL("PRAGMA foreign_keys = ON;");
}
return this;
}
private void close() {
dbHelper.close();
}
}
当我调用数据库方法时,我会做以下事情:
public Object getData() {
open();
//... database operations take place ...
close();
return data;
}
但正如我所说,我仍然收到此 SQLite 连接泄漏警告。
我究竟做错了什么?