Managing SQLite Database

Source: Internet
Author: User
Tags sqlite sqlite database


Approach #1: Use a Singleton to instantiate the SQLiteOpenHelper


Declare your database helper as a static instance variable and use the Singleton pattern to guarantee the Singleton proper Ty. The sample code below should give you a good idea on how to go about designing the DatabaseHelperclass correctly.


The static getInstance()Method ensures.DatabaseHelperWould ever exist at any given time. If thesInstance object have not been initialized, one'll be created. If one has already been created then it'll simply be returned.You should not initialize your helper object using with new DatabaseHelper(context)! Instead, always use DatabaseHelper.getInstance(context), as it guarantees the one database helper would exist across the entire applicatio N ' s 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.
    // See this article for more information: http://bit.ly/6LRzfx
    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);
  }
}
Approach #2: Wrap theIn SQLiteDatabasea ContentProvider

This was also a nice approach. For one, the newCursorLoaderClass requires ContentProviders, so if you want a Activity or Fragment to implementLoaderManager.LoaderCallbacks<Cursor>With aCursorLoader(As discussed inThis post), you'll need to implement a ContentProviderFor your application. Further, you don ' t need to worry about making a singleton database helper withContentProviderS. Simply call from the getContentResolver()A Ctivity and the system would take care of everything for your (in other words, there are no need for designing a Singleton PA Ttern to prevent multiple instances from being created).





Reprint: http://www.androiddesignpatterns.com/2012/05/correctly-managing-your-sqlite-database.html



Managing SQLite Database


Related Article

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.