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

这是使用DBManager一个的AsyncTask:

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()onPostExecute关闭数据库。

我以为我可以在dbMan.getAllForms()添加db.open()和db.close(),以避免每次我用dbMan.getAllForms()时间调用它。

您对此有何看法?什么是最好的方法?

+0

您应该看看装载机:http://developer.android.com/guide/topics/fundamentals/loaders.html – 2012-04-19 10:05:09

回答

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()),并在您离开活动后关闭它。然后将Dbmanager传递给AsyncTask。

0

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