Managing SQLite Database

來源:互聯網
上載者:User

標籤:

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 property. The sample code below should give you a good idea on how to go about designing the DatabaseHelper class correctly.

The static getInstance() method ensures that only one DatabaseHelper will ever exist at any given time. If the sInstance object has not been initialized, one will be created. If one has already been created then it will simply be returned. You should not initialize your helper object using with new DatabaseHelper(context)! Instead, always use DatabaseHelper.getInstance(context), as it guarantees that only one database helper will exist across the entire application’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 the  SQLiteDatabase in a  ContentProvider

This is also a nice approach. For one, the new CursorLoader class requires ContentProviders, so if you want an Activity or Fragment to implement LoaderManager.LoaderCallbacks<Cursor> with aCursorLoader (as discussed in this post), you’ll need to implement a ContentProvider for your application. Further, you don’t need to worry about making a singleton database helper withContentProviders. Simply call getContentResolver() from the Activity and the system will take care of everything for you (in other words, there is no need for designing a Singleton pattern to prevent multiple instances from being created).

 

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

Managing SQLite Database

聯繫我們

該頁面正文內容均來源於網絡整理,並不代表阿里雲官方的觀點,該頁面所提到的產品和服務也與阿里云無關,如果該頁面內容對您造成了困擾,歡迎寫郵件給我們,收到郵件我們將在5個工作日內處理。

如果您發現本社區中有涉嫌抄襲的內容,歡迎發送郵件至: info-contact@alibabacloud.com 進行舉報並提供相關證據,工作人員會在 5 個工作天內聯絡您,一經查實,本站將立刻刪除涉嫌侵權內容。

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.