FC_Inter_Fan
FC_Inter_Fan

Reputation: 132

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 explicitly called. If I have created the database and then add the close() those errors will go away, but I cannot reinstall my app and run it with that.

private static class DbHelper extends SQLiteOpenHelper {

    public DbHelper(Context context) {
        super(context, DATABASE_score, null, DATABASE_VERSION);
        // TODO Auto-generated constructor stub
    }

    @Override
    public void onCreate(SQLiteDatabase db) {
        // TODO Auto-generated method stub

        db.execSQL("CREATE TABLE " + DATABASE_TABLE + " (" + KEY_ROWID
                + " INTEGER PRIMARY KEY AUTOINCREMENT UNIQUE, " + KEY_SCORE1
                + " INTEGER TEXT NOT NULL, " + KEY_SCORE2
                + " INTEGER TEXT NOT NULL);");
        db.close(); <---problem
    }

    @Override
    public void onUpgrade(SQLiteDatabase db, int oldVersion, int newVersion) {
        // TODO Auto-generated method stub
        db.execSQL("DROP TABLE IF EXISTS " + DATABASE_TABLE);
        onCreate(db);
    }
}

public Database(Context c) {
    ourContext = c;
}

public Database open() throws SQLException {
    ourHelper = new DbHelper(ourContext);
    ourDatabase = ourHelper.getWritableDatabase();
    return this;
}

public void close() {
    ourHelper.close();
}

public long createEntry(String score, String score2) {
    // TODO Auto-generated method stub
    ContentValues cv = new ContentValues();
    cv.put(KEY_SCORE1, score);
    cv.put(KEY_SCORE2, score2);

    return ourDatabase.insert(DATABASE_TABLE, null, cv);
}

So the question is: how do I call db.close() and have my app run on correctly?

Upvotes: 1

Views: 386

Answers (3)

hovanessyan
hovanessyan

Reputation: 31483

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).

Upvotes: 2

AD14
AD14

Reputation: 1218

call super.close();

 if(myDataBase != null)
        myDataBase.close();

       super.close();

Upvotes: 0

Dawid Sajdak
Dawid Sajdak

Reputation: 3084

private Context context;
private SQLiteDatabase db;

public DataBase(Context context) {
    this.context = context;
    OpenHelper openHelper = new OpenHelper(this.context);
    this.db = openHelper.getWritableDatabase();
}

public void closeData() {
    this.db.close();
}

try this ;)

Upvotes: 0

Related Questions