Sign Up

Sign Up to our social questions and Answers Engine to ask questions, answer people’s questions, and connect with other people.

Have an account? Sign In

Have an account? Sign In Now

Sign In

Login to our social questions & Answers Engine to ask questions answer people’s questions & connect with other people.

Sign Up Here

Forgot Password?

Don't have account, Sign Up Here

Forgot Password

Lost your password? Please enter your email address. You will receive a link and will create a new password via email.

Have an account? Sign In Now

You must login to ask a question.

Forgot Password?

Need An Account, Sign Up Here

Please briefly explain why you feel this question should be reported.

Please briefly explain why you feel this answer should be reported.

Please briefly explain why you feel this user should be reported.

Sign InSign Up

The Archive Base

The Archive Base Logo The Archive Base Logo

The Archive Base Navigation

  • Home
  • SEARCH
  • About Us
  • Blog
  • Contact Us
Search
Ask A Question

Mobile menu

Close
Ask a Question
  • Home
  • Add group
  • Groups page
  • Feed
  • User Profile
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Buy Points
  • Users
  • Help
  • Buy Theme
  • SEARCH
Home/ Questions/Q 7806479
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: June 2, 20262026-06-02T02:32:59+00:00 2026-06-02T02:32:59+00:00

I’m developing an android 3.1 application. This question is not specific for Android, it

  • 0

I’m developing an android 3.1 application.

This question is not specific for Android, it is about how to design a class that access a database. I asked here because my code is for Android.

I have a class, DBManager, to work with Sqlite database. This is a part of its implementation:

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

And this is an AsyncTask that uses 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();
    }
}

As you can see I have to:

  1. Create DBManager instance.
  2. Open database with dbMan.open()
  3. Call dbMan.getAllForms()
  4. Close database with dbMan.close() on onPostExecute.

I thought that I could add db.open() and db.close() on dbMan.getAllForms() to avoid calling it every time I use dbMan.getAllForms().

What do you think about this? What is the best approach?

  • 1 1 Answer
  • 0 Views
  • 0 Followers
  • 0
Share
  • Facebook
  • Report

Leave an answer
Cancel reply

You must login to add an answer.

Forgot Password?

Need An Account, Sign Up Here

1 Answer

  • Voted
  • Oldest
  • Recent
  • Random
  1. Editorial Team
    Editorial Team
    2026-06-02T02:33:01+00:00Added an answer on June 2, 2026 at 2:33 am

    I would put it inside getAllForms() or do something like that

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

    Since you don’t need the db connection after you have the result you can close it right away.

    Edit: if you run that AsyncTask several times then opening/closing will introduce unnecessary overhead. In that case you may want to instanciate the dbManager from your Activity (maybe open() in the constructor of DbManager) and close it once you leave your activity. Then pass Dbmanager to the AsyncTask.

    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

I'm parsing an RSS feed that has an &#8217; in it. SimpleXML turns this
I have a string like this: La Torre Eiffel paragonata all&#8217;Everest What PHP function
I am doing a simple coin flipping experiment for class that involves flipping a
I need a function that will clean a strings' special characters. I do NOT
link Im having trouble converting the html entites into html characters, (&# 8217;) i
That's pretty much it. I'm using Nokogiri to scrape a web page what has
For some reason, after submitting a string like this Jack’s Spindle from a text
this is what i have right now Drawing an RSS feed into the php,
I've got a string that has curly quotes in it. I'd like to replace
I am reading a book about Javascript and jQuery and using one of the

Explore

  • Home
  • Add group
  • Groups page
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Users
  • Help
  • SEARCH

Footer

© 2021 The Archive Base. All Rights Reserved
With Love by The Archive Base

Insert/edit link

Enter the destination URL

Or link to existing content

    No search term specified. Showing recent items. Search or use up and down arrow keys to select an item.