zoukankan      html  css  js  c++  java
  • Managing 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 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

  • 相关阅读:
    图论 拓扑排序
    图论 k短路
    图论 最短路 spfa
    12.14 操作系统实验:linux虚拟机与进程管理
    Anaconda 换用清华园后安装速度依然很慢,或者安装包出错
    6.21 在panel中设置背景并不覆盖控件--paintComponent
    1.15 关于String类型和其他主数据类型相互转换的方法 (转)
    1.14 Headfirstjava第五章 简单游戏代码
    1.14 HeadFirstJava 前六章读书笔记总结
    1.22训练赛 --ac2
  • 原文地址:https://www.cnblogs.com/liaojie970/p/5802413.html
Copyright © 2011-2022 走看看