Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

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?

like image 446
Bishan Avatar asked Dec 07 '12 09:12

Bishan


People also ask

When should a SQLite database be closed *?

if the users of your application have the ability to create many database interactions very quickly, you should use something like i have demonstrated above. but if there is minimal database interactions, i wouldn't worry about it, and just create and close the database every time.

Why is my database closed android studio?

For me the reason for "closed" was - most probably - that the framework I use (Exposed/SQLDroid) closes the database connection when it is not working. So, even when everything worked fine in the application, the database was closed at the time the Database Inspector wanted to look into it.


1 Answers

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.

like image 77
JustDanyul Avatar answered Oct 01 '22 03:10

JustDanyul