0

我正在开发一个 android 3.1 应用程序。

这个问题不是针对Android的,它是关于如何设计一个访问数据库的类。我在这里问是因为我的代码是针对 Android 的。

我有一堂课,DBManager可以使用Sqlite 数据库。这是其实现的一部分:

public class DBManager
{
    // Variable to hold the database instance
    private SQLiteDatabase db;
    // Database open/upgrade helper
    private DatabaseHelper dbHelper;

    public DBManager(Context _context)
    {
        Log.v("DBManager", "constructor");
        dbHelper = new DatabaseHelper(_context, SqlConstants.DATABASE_NAME, null, SqlConstants.DATABASE_VERSION);
    }

    public DBManager open() throws SQLException
    {
        Log.v("DBManager", "open");
        db = dbHelper.getWritableDatabase();
        return this;
    }

    public void close()
    {
        Log.v("DBManager", "close");
        db.close();
    }

        ...

        /**
     * Query all forms available locally.
     * @return A list with all forms (form.name and form.FormId) available on local db
     * or null if there was a problem.
     */
    public ArrayList<Form> getAllForms()
    {
        Log.v("DBManager", "getAllForms");
        ArrayList<Form> list = null;
        Cursor c = null;

        try
        {
            c = this.getAllFormsCursor();
            if (c != null)
            {
                int formNameIndex = c.getColumnIndex(SqlConstants.COLUMN_FORM_NAME);
                int formIdIndex = c.getColumnIndex(SqlConstants.COLUMN_FORM_ID);

                c.moveToFirst();
                if (c.getCount() > 0)
                {
                    list = new ArrayList<Form>(c.getCount());
                    do
                    {
                        Form f = new Form();
                        f.Name = c.getString(formNameIndex);
                        f.FormId = c.getString(formIdIndex);
                        list.add(f);
                    }
                    while (c.moveToNext());
                }
            }
        }
        catch (Exception e)
        {
            e.printStackTrace();
            list = null;
        }
        finally
        {
            if (c != null)
                c.close();
        }
        return list;
    }

    private Cursor getAllFormsCursor()
    {
        Log.v("DBManager", "getAllFormsCursor");

        return db.query(SqlConstants.TABLE_FORM,
                new String[] {
                SqlConstants.COLUMN_FORM_ID,
                SqlConstants.COLUMN_FORM_NAME}, null, null, null, null, null);
    }
}

这是一个 AsyncTask 使用DBManager

private class DbFormListAsyncTask extends AsyncTask<Void, Void, ArrayList<Form>>
{
    private Context mContext;
    private ProgressDialog loadingDialog;
    private DBManager dbMan;

    DbFormListAsyncTask(Context context)
    {
        this.mContext = context;
        loadingDialog = new ProgressDialog(mContext);
        loadingDialog.setProgressStyle(ProgressDialog.STYLE_SPINNER);
        loadingDialog.setMessage("Retriving forms. Please wait...");
        loadingDialog.setCancelable(false);
        loadingDialog.show();
    }

    @Override
    protected ArrayList<Form> doInBackground(Void... arg0)
    {
        dbMan = new DBManager(mContext);
        dbMan.open();

        return dbMan.getAllForms();
    }

    protected void onPostExecute(ArrayList<Form> forms)
    {
        if (forms != null)
        {
            ListActivity act = (ListActivity) mContext;
            act.setListAdapter(new AvaFormAdapter(act, R.layout.ava_list_item, forms));
        }
        else
        {
            TextView errorMsg = (TextView)
                    ((FormsListActivity) mContext).findViewById(R.id.formErrorMsg);
            errorMsg.setText("Problem getting forms. Please try again later.");
        }
        loadingDialog.dismiss();
        if (dbMan != null)
            dbMan.close();
    }
}

如您所见,我必须:

  1. 创建 DBManager 实例。
  2. 打开数据库dbMan.open()
  3. 称呼dbMan.getAllForms()
  4. dbMan.close()使用on关闭数据库onPostExecute

我认为我可以添加 db.open() 和 db.close()dbMan.getAllForms()以避免每次使用dbMan.getAllForms().

你怎么看待这件事?最好的方法是什么?

4

2 回答 2

0

我会把它放在里面getAllForms()或做类似的事情

protected ArrayList<Form> doInBackground(Void... arg0)
{
    dbMan = new DBManager(mContext);
    dbMan.open();
    ArrayList<Form> resutl = dbMan.getAllForms();
    dbMan.close();
    return result;
}

由于您在获得结果后不需要数据库连接,您可以立即将其关闭。

编辑:如果您多次运行该 AsyncTask ,则打开/关闭将引入不必要的开销。在这种情况下,您可能希望从您的 Activity 中实例化 dbManager(可能是 DbManager 的构造函数中的 open()),并在您离开 Activity 后将其关闭。然后将 Dbmanager 传递给 AsyncTask。

于 2012-04-19T10:08:09.713 回答
0

使您的数据库助手类成为单例,并且不要显式关闭SQLiteDatabase. 当您的应用程序的进程退出时,它将被关闭并刷新。

于 2012-04-20T02:33:14.433 回答