正确的方法打开/关闭数据库? [英] Correct way to open/close the database?

查看:160
本文介绍了正确的方法打开/关闭数据库?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

现在我使用 SQLOpenHelper 类的静态实例,如下所示:

Right now I am using a static instance of the SQLOpenHelper class like so:

public class DatabaseHelper extends SQLiteOpenHelper {
    private static DatabaseHelper mInstance = null;
    private final Context mContext;

    //...

    public static synchronized DatabaseHelper getInstance(Context context) {
        /**
         * use the application context as suggested by CommonsWare.
         * this will ensure that you don't accidentally leak an Activity's
         * context (see this article for more information:
         * http://android-developers.blogspot.nl/2009/01/avoiding-memory-leaks.html)
         */
        if (mInstance == null) {
            mInstance = new DatabaseHelper(context.getApplicationContext());
        }
        return mInstance;
    }

    private DatabaseHelper(Context context) {
        super(context, DB_NAME, null, DB_VERSION);
        mContext = context;
    }

    //...
}

然后一个 DatabaseProcessor 类像这样:

public class DatabaseProcessor {

    private SQLiteDatabase mDatabase;
    private DatabaseHelper mSQLHelper;
    private Context mContext;

    public DatabaseProcessor(Context context) {
        mContext = context;
        mSQLHelper = DatabaseHelper.getInstance(mContext);
    }

    public void open() throws SQLException {
        mDatabase = mSQLHelper.getWritableDatabase();
    }

    public void close() {
        mDatabase.close();
    }

   //...
}

因此,如果我想访问我的数据库,我做这样的事情:

So if I want to access my database, I do something like this:

DatabaseProcessor mDatabaseProcessor = new DatabaseProcessor(this);
mDatabaseProcessor.open();
mSomeList = mDatabaseProcessor.doSomeQueryAndReturnResults();
mDatabaseProcessor.close();

这是正确的方法吗?或者最好在基本 Activity onResume()方法中打开数据库,并在onPause()期间关闭它?

Is this the correct way to do this? Or is it better to open the database in the base Activity onResume() method and close it during onPause()? How do I correctly throw errors for situations where the database is not open when I try to run a query?

EDIT 重构的版本:

public class DatabaseHelper extends SQLiteOpenHelper {
    private static SQLiteDatabase mDatabase;
    private static DatabaseHelper mInstance = null;
    private static Context mContext;

    // ...

    public static synchronized DatabaseHelper getInstance(Context context) {
        /**
         * use the application context as suggested by CommonsWare.
         * this will ensure that you don't accidentally leak an Activity's
         * context (see this article for more information:
         * http://android-developers.blogspot.nl/2009/01/avoiding-memory-leaks.html)
         */
        if (mInstance == null) {
            mInstance = new DatabaseHelper(context.getApplicationContext());
        }
        return mInstance;
    }

    private DatabaseHelper(Context context) {
        super(context, DB_NAME, null, DB_VERSION);
        mContext = context;
    }

    @Override
    public void onCreate(SQLiteDatabase db) {
        db.execSQL(DB_CREATE_SOME_TABLE); //some SQL expression
    }

    @Override
    public void onUpgrade(SQLiteDatabase db, int oldVersion, int newVersion) { 
        db.execSQL(DB_ALTER);
    }

    public void open() throws SQLException {
        mDatabase = getWritableDatabase();
    }

    public void close() {
        mDatabase.close();
    }

    public boolean isOpen() {
        return mDatabase.isOpen();
    }

    //below this would be various CRUD functions operating on mDatabase
    // ...
    // ...
}


推荐答案

最好的方法是将你的查询/ try-catch ,然后释放所有资源并在最后中关闭连接。

The best way would be to put your query/transaction statements in try-catch and then release all your resources and close the connection in finally block.

try{
      mSomeList = mDatabaseProcessor.doSomeQueryAndReturnResults();
} catch(Exception exc){
    //Catch exceptions here
} 
finally{
    if(mDatabaseProcessor != null)
         mDatabaseProcessor.close();
}

这篇关于正确的方法打开/关闭数据库?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆