数据库全局实例

dim*_*ika 6 database sqlite android android-loadermanager

所以我希望为所有应用程序活动都有一个数据库实例.我找到了以下代码:

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;
    }
}
Run Code Online (Sandbox Code Playgroud)

我什么时候可以关闭SQLiteDatabase实例,我不明白.

Ale*_*ood 13

当我刚开始使用Android时,这对我来说是一个问题,因为网上没有很多教程描述如何正确地允许在整个应用程序中访问您的数据库(不要问我原因).这里有一些展示三种可能方法的示例代码.

方法#1:继承`Application`

如果你知道你的应用程序不会很复杂(例如,如果你知道你最终只有一个子类Application),那么你可以创建一个子类Application并让你的主Activity扩展它.这可确保数据库的一个实例在整个应用程序的整个生命周期中运行.

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;
    }
}
Run Code Online (Sandbox Code Playgroud)

方法#2:让`SQLiteOpenHelper`成为静态数据成员

这不是完整的实现,但它应该让您对如何DatabaseHelper正确设计类有一个很好的了解.静态工厂方法确保任何时候只存在一个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;
    }
}
Run Code Online (Sandbox Code Playgroud)

方法#3:使用`ContentProvider`抽象SQLite数据库

这是我建议的方法.首先,新LoaderManager类很大程度上依赖于ContentProviders,所以如果你想要实现一个Activity或Fragment LoaderManager.LoaderCallbacks<Cursor>(我建议你利用它,这很神奇!),你需要ContentProvider为你的应用程序实现一个.此外,您不必担心使用ContentProviders创建Singleton数据库帮助程序.只需getContentResolver()从Activity 调用,系统就会为您处理所有事情(换句话说,不需要设计Singleton模式来防止创建多个实例).

希望有所帮助!