android - 数据库全局实例

标签 android database sqlite android-loadermanager

所以我想为所有应用程序 Activity 创建一个数据库实例。 我找到了以下代码:

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;
    }
}

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

最佳答案

当我刚开始使用 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;
    }
}

方法 #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;
    }
}

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

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

希望对您有所帮助!

关于android - 数据库全局实例,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/7277087/

相关文章:

android - 阿拉伯语的 DatePickerDialog

android - 使用小部件为 Android 应用评分

php - 如何解决与使用 php 将条目插入 mysql 相关的问题?

javascript - 如何在vuetify项目中更新firebase文档数据onclick

android - 如何在 ListView 中传递带有项目的标识符?

android - 我是否应该在参数中传递数据库用户和密码来调用 Web 服务以从数据库中获取数据

android - 更新到 Android 6.0 后,SQLite 数据库崩溃

concurrency - SQLite中并发读/写和读的策略

python - 将文件中的数据插入 SQLite 数据库

android - 如何处理取消注册推送通知?