Search code examples
androidsqliteandroid-sqlite

Check sqlite database is open or closed in android


I have two methods in my DBDriver class to open and close sqlite database as below.

private SQLiteDatabase database;

/**
     * Open Database to read and write.
     * 
     * @throws SQLException
     */
    public void open() throws SQLException {
        database = dbHelper.getWritableDatabase();

    }

    /**
     * Close opened database.
     */
    public void close() {
        dbHelper.close();
    }

I'm using above methods in my other class to open and close sqlite database.

I want to identify database is open or closed. How could I do this?


Solution

  • You can use isOpen() to check, so in your case, that would be

    database.isOpen()
    

    just a tip, when working with Java based API (or any APIs for that matter), learning to use the documentation of the API is key. The docs will tell you which methods are available for any given class. For example, you got a instance of the class SQLiteDatabase. Checking the javadocs below

    http://developer.android.com/reference/android/database/sqlite/SQLiteDatabase.html

    its pretty trivial to identify the method you are looking for.