Closing My Database Correctly
I cannot create my database when I close it (check code below). If I do not close it everything works fine but I get several errors in logcat saying that close() was never explicit
Solution 1:
You don't have to call db.close();
inside your onCreate()
in your DbHelper class.
Instead you have to acquire a database instance from your DbHelper class using
DbHelper_instance.getReadableDatabase()
or DbHelper_instance.getWritableDatabase()
and assign that to SQLiteDatabase object (let's define it as SQLiteDatabase database).
After you finish using the database, and you close your cursor, you can do something like:
if (database.isOpen()) {
Log.d(TAG + " Closing SQLite Database connection...");
database.close();
}
You can put that in finally block, to be sure it will always execute (taking in mind that your DB operations are surrounded in try/catch).
Solution 2:
private Context context;
private SQLiteDatabase db;
publicDataBase(Context context) {
this.context = context;
OpenHelper openHelper = new OpenHelper(this.context);
this.db = openHelper.getWritableDatabase();
}
publicvoidcloseData() {
this.db.close();
}
try this ;)
Solution 3:
call super.close();
if(myDataBase != null)
myDataBase.close();
super.close();
Post a Comment for "Closing My Database Correctly"