title: Using Singleton pattern for Sqlite database in Android
body: <p>I am a little confused how to do this properly. Here is my DatabaseHelper class:</p>
  
  <pre><code>public class DatabaseHelper extends SQLiteOpenHelper {
      private static DatabaseHelper mInstance = null;
  
      private static final String DB_NAME = "database.db";
      private static final int DB_VERSION = 1; 
  
      private Context mContext;
  
      public static DatabaseHelper getInstance(Context context) {
  
          if (mInstance == null) {
              mInstance = new DatabaseHelper(context.getApplicationContext());
          }
          return mInstance;
      }
  
      public DatabaseHelper(Context context) {
          super(context, DB_NAME, null, DB_VERSION);
          mContext = context;
      }
  
    ...
  </code></pre>
  
  <p>Assuming this is right, is this the correct way to handle the querying class:</p>
  
  <pre><code>public class DatabaseProcessor {
  
      private SQLiteDatabase mDatabase;
      private DatabaseHelper mSQLHelper;
      private Context mContext;
  
      public DatabaseProcessor(Context context) {
          mContext = context;
          mSQLHelper = new DatabaseHelper(mContext);
      }
  
      private void open() throws SQLException {
          mDatabase = mSQLHelper.getWritableDatabase();
      }
  
      private void close() {
          mDatabase.close();
      }
  
      public void insertSomethingIntoDb(String key) {
          ContentValues values = new ContentValues();
          values.put("some_column_name", name);
          open();
          mDatabase.insert("some_table_name", null, values);
          close();
      }
  ...
  </code></pre>
  
  <p>And if this is right, how do I properly invoke a db method from somewhere else in the code such as an Activity, Fragment, etc, anywhere.</p>