Global sample database

Therefore, I want to have one database instance for all applications. I found the following code:

public class MyApplication extends Application { private static SQLiteDatabase mDB = null; @Override public void onCreate() { super.onCreate(); DataBaseOpenHelper m_OpenHelper = new DataBaseOpenHelper( this ); mDB = m_OpenHelper.getWritableDatabase(); } public static SQLiteDatabase getDB() { return mDB; } } 

I do not understand when I can close an instance of SQLiteDatabase.

+6
source share
3 answers

This was a problem for me when I first started working with Android, as there are not many tutorials on the Internet that describe how to correctly allow access to your database throughout the application (do not ask me why). Here is an example code that shows three possible approaches.

Approach # 1: Subclassification of `Application`

If you know that your application will not be very complex (i.e. if you know that you will have only one subclass of Application ), then you can create a subclass of Application and expand your main activity of This. This ensures that one instance of the database runs throughout the application life cycle.

 public class MainApplication extends Application { /** * see NotePad tutorial for an example implementation of DataDbAdapter */ private static DataDbAdapter mDbHelper; /** * create the database helper when the application is launched */ @Override public void onCreate() { mDbHelper = new DataDbAdapter(this); mDbHelper.open(); } /** * close the database helper when the application terminates. */ @Override public void onTerminate() { mDbHelper.close(); mDbHelper = null; } public static DataDbAdapter getDatabaseHelper() { return mDbHelper; } } 

Approach # 2: there is `SQLiteOpenHelper` - a static data member

This is not a complete implementation, but it should give you a good idea on how to plan the DatabaseHelper class correctly. The static factory method ensures that at any time there is only one instance of DatabaseHelper.

 /** * create custom DatabaseHelper class that extends SQLiteOpenHelper */ public class DatabaseHelper extends SQLiteOpenHelper { private static DatabaseHelper mInstance = null; private static final String DATABASE_NAME = "databaseName"; private static final String DATABASE_TABLE = "tableName"; private static final int DATABASE_VERSION = 1; private Context mCxt; public static DatabaseHelper getInstance(Context ctx) { /** * use the application context as suggested by CommonsWare. * this will ensure that you dont accidentally leak an Activitys * context (see this article for more information: * http://developer.android.com/resources/articles/avoiding-memory-leaks.html) */ if (mInstance == null) { mInstance = new DatabaseHelper(ctx.getApplicationContext()); } return mInstance; } /** * constructor should be private to prevent direct instantiation. * make call to static factory method "getInstance()" instead. */ private DatabaseHelper(Context ctx) { super(context, DATABASE_NAME, null, DATABASE_VERSION); this.mCtx = ctx; } } 

Approach # 3: SQLite Database Annotation Using ContentProvider

This is the approach I would suggest. Firstly, the new LoaderManager class is heavily dependent on ContentProviders, so if you want to implement Activity or Fragment LoaderManager.LoaderCallbacks<Cursor> (which I suggest you use, it's magic!), You will need to implement ContentProvider for your application. In addition, you do not need to worry about creating a Singleton database assistant with ContentProviders. Just call getContentResolver() from the Activity, and the system will take care of everything for you (in other words, there is no need to develop a Singleton template to prevent multiple instances from being created).

Hope this helps!

+13
source

You really do not need to close it. It automatically closes when your process dies. Instead of using the Application object, you can simply make your auxiliary DB object one of them for easier access. BTW, getWritableDatabase() and getReadableDatabase() not so different. The only difference is that getReadableDatabase() may work if you are out of place, while the other will throw an exception.

+2
source

I do not think it is recommended, if anything, you can have a global instance of DataBaseOpenHelper, and call getWritableDatabase () at the points where you really need the database. And, obviously, only get a writeable instance when you need it, otherwise use getReadableDatabase ().

I am not sure what exactly is the reason for this, but I assume that it is an expensive object to receive and / or hold, so you do not want to create it when the application is loaded and held before the global instance. Maybe someone else can clarify this point. I would like the Android docs to explain this better, because I also had the momentum for that.

0
source

Source: https://habr.com/ru/post/896449/


All Articles