我遇到SQLite连接问题。当我的Activity加载时,logCat说:
A SQLiteConnection object for database '/data/data/org.witixradio.app/databases/database.db' was leaked! Please fix your application to end transactions in progress properly and to close the database when it is no longer needed.
我检查了我的数据库访问权限,但我找不到保持连接打开的位置。
这是我的代码:
public ListeurPlanning(Activity _activity, ListView _view, String jour) {
PlanningDAO listing = new PlanningDAO(activity);
Cursor c = listing.selectionnerDay(jour);
c.moveToFirst();
if(c.getCount()<=0) {
repertoire[0][0] = "Playlist";
repertoire[0][1] = "0h-24h";
repertoire[0][2] = "Playlist";
}else {
// I do a lot of stuff with my cursor
}
c.close();
我在使用后关闭光标,所以我不认为我的问题在这里。 现在是PlanningDAO类的代码:
public class PlanningDAO extends DAOBasePlanning{
public static final String PLANNING_KEY = "id";
public static final String PLANNING_JOUR = "jour";
public static final String PLANNING_HEURE_DEBUT = "heuredebut";
public static final String PLANNING_HEURE_FIN = "heurefin";
public static final String PLANNING_ANIM = "anim";
public static final String PLANNING_EMISSION = "emission";
public static final String PLANNING_TABLE_NAME = "Planning";
public static final String PLANNING_TABLE_CREATE =
"CREATE TABLE " + PLANNING_TABLE_NAME + " (" +
PLANNING_KEY + " INTEGER PRIMARY KEY AUTOINCREMENT, " +
PLANNING_JOUR + " TEXT, " +
PLANNING_HEURE_DEBUT + " INTEGER, " +
PLANNING_HEURE_FIN + " INTEGER, " +
PLANNING_ANIM + " TEXT, " +
PLANNING_EMISSION + " TEXT)";
public static final String PLANNING_TABLE_DROP = "DROP TABLE IF EXISTS " + PLANNING_TABLE_NAME + ";";
/**
* le métier à ajouter à la base
*/
PlanningDAO(Context pContext) {
super(pContext);
}
public void ajouter(Planning p) {
ContentValues value = new ContentValues();
value.put(PlanningDAO.PLANNING_JOUR, p.getJour());
value.put(PlanningDAO.PLANNING_HEURE_DEBUT, p.gethoraireDebut());
value.put(PlanningDAO.PLANNING_HEURE_FIN, p.gethoraireFin());
value.put(PlanningDAO.PLANNING_ANIM, p.getAnimateur());
value.put(PlanningDAO.PLANNING_EMISSION, p.getEmission());
mDb.insert(PlanningDAO.PLANNING_TABLE_NAME, null, value);
}
/**
* @param id l'identifiant du métier à supprimer
*/
public void supprimer(long id) {
mDb.delete(PLANNING_TABLE_NAME, PLANNING_KEY + " = ?", new String[] {String.valueOf(id)});
}
public void purge(){
mDb.delete(PLANNING_TABLE_NAME,null,null);
}
/**
* @param id l'identifiant du métier à récupérer
*/
public Cursor selectionner(long id) {
Cursor c = mDb.rawQuery("select * from " + PLANNING_TABLE_NAME + " where id = ?", new String[]{Long.toString(id)});
return c;
}
public Cursor selectionnerAll() {
Cursor c = mDb.rawQuery("select * from " + PLANNING_TABLE_NAME + " where id > ?", new String[]{"0"});
return c;
}
public Cursor selectionnerDay(String jour){
Cursor c = mDb.rawQuery("select * from " + PLANNING_TABLE_NAME + " where jour = ? ORDER BY heuredebut", new String[]{jour});
return c;
}
}
现在我的DAOBasePlanning类:
public abstract class DAOBasePlanning {
// Nous sommes à la première version de la base
// Si je décide de la mettre à jour, il faudra changer cet attribut
protected final static int VERSION = 3;
// Le nom du fichier qui représente ma base
protected final static String NOM = "database.db";
protected SQLiteDatabase mDb = null;
protected DatabaseHandlerPlanning mHandler = null;
public DAOBasePlanning(Context pContext) {
this.mHandler = new DatabaseHandlerPlanning(pContext, NOM, null, VERSION);
}
public SQLiteDatabase open() {
// Pas besoin de fermer la dernière base puisque getWritableDatabase s'en charge
mDb = mHandler.getWritableDatabase();
return mDb;
}
public void close() {
mDb.close();
}
public SQLiteDatabase getDb() {
return mDb;
}
}
我已经阅读了很多关于这个主题的问题,但我找不到问题的原因
也许你有个主意。
非常感谢
答案 0 :(得分:0)
您永远不会在close()
对象上调用PlanningDAO
。因此,它的mDB
引用永远不会被关闭 - 并且泄漏。