1

i searched lot questions but i didnt get correct answer. What is the best way to open and close the database in activity lifecycle. please someone help me with correct answer.

Thanks in advance.

  • use singleton pattern for that so you dont need to handle it in activity lifecycle. http://www.androiddesignpatterns.com/2012/05/correctly-managing-your-sqlite-database.html – Pramod mishra Jun 01 '16 at 07:56

2 Answers2

3

Use Singleton pattern and access using db=DatabaseHelper.getInstance(context). It guarantees that only one database helper will exist across the entire application lifecycle.

public class DatabaseHelper extends SQLiteOpenHelper { 

  private static DatabaseHelper sInstance;

  private static final String DATABASE_NAME = "database_name";
  private static final String DATABASE_TABLE = "table_name";
  private static final int DATABASE_VERSION = 1;

  public static synchronized DatabaseHelper getInstance(Context context) {

    // Use the application context, which will ensure that you 
    // don't accidentally leak an Activity's context.
    if (sInstance == null) {
      sInstance = new DatabaseHelper(context.getApplicationContext());
    }
    return sInstance;
  }

  /**
   * Constructor should be private to prevent direct instantiation.
   * make call to static method "getInstance()" instead.
   */
  private DatabaseHelper(Context context) {
    super(context, DATABASE_NAME, null, DATABASE_VERSION);
  }
}

And access using :

db=DatabaseHelper.getInstance(this);

And also you can close database connection in catch block if needed. I hope it help.

Govinda P
  • 3,261
  • 5
  • 22
  • 43
  • While this answer is good, please explain for the user `when` should connections be opened and closed. As far as I could read, the user expects to use the database during an application, and desires to know when to start the connection, and when to end it. For instance, the database should open in "MainActivity".`onCreate()`, and close on "MainActivity".`onDestroy`, this way, the database is usable throughout the entire stack, while being managed in a single point. Otherwise, it could be done in ApplicationContext as well, but the closing would be harder (without knowing the app) – Bonatti Jun 01 '16 at 12:06
  • 1
    Yes correct user can open in onCreate() and close after use or in onStop. But please check this answer http://stackoverflow.com/a/7739454/5275436. according to this post there's nothing wrong with leaving the database connection open. So it better way to handle single db instance in application. – Govinda P Jun 01 '16 at 12:19
1

You can open database like this

public void openDataBase() throws SQLException {
        String myPath = DB_PATH + DB_NAME;
        myDataBase = SQLiteDatabase.openDatabase(myPath, null,
                SQLiteDatabase.OPEN_READWRITE);
        }

close database

 public synchronized void close() {
        if (myDataBase != null)
            myDataBase.close();
        //you need to extend the class with SQLiteOpenHelper
         super.close();
    }
Jinesh Francis
  • 3,377
  • 3
  • 22
  • 37