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

  • SEARCH
  • Home
  • 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 5947787
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 22, 20262026-05-22T17:00:50+00:00 2026-05-22T17:00:50+00:00

What is considered to be best practice for handling database connections? (I omitted the

  • 0

What is considered to be best practice for handling database connections? (I omitted the constructor and onUpgrade method within the DatabaseHelper class) These are just 2 ways I found on the internet, perhaps you have a better way of handling? I would love to hear.

Option 1

public class DatabaseManager {

private SQLiteDatabase mDb;

public DatabaseManager(Context context) {

   DatabaseHelper helper = new DatabaseHelper(context);
   helper.getWritableDatabase();
}

// ... methods that use mDb

private class DatabaseHelper extends SQLiteOpenHelper {

   @Override
   public void onCreate(SQLiteDatabase db) {

      mDb = db;
      //create database
   }

   @Override
   public void onOpen(SQLiteDatabase db) {

      mDb = db;
   }
}

}

Option 2

public class DatabaseManager {

private DatabaseHelper mDbHelper;

public DatabaseManager(Context context) {

   mDbHelper = new DatabaseHelper(context);
}

// ... methods that fetch the db

private void sampleMethod() {
   SQLiteDatabase db = mDbHelper.getWritableDatabase();
   //do stuff with database
   mDbHelper.close();
}

private static class DatabaseHelper extends SQLiteOpenHelper {

   @Override
   public void onCreate(SQLiteDatabase db) {

      //create database
   }
}

}

Also, is it needed to call close() everytime you used the database within option 2? As for using option 1, I guess you need to call close() when the app’s onDestroy is called?

  • 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-05-22T17:00:51+00:00Added an answer on May 22, 2026 at 5:00 pm

    I used to worry about all this but recently I started using ORMLite. It is a very light ORM with Android libraries and saves you worrying about this kind of stuff.

    I would say that this could very soon become best practice as it removes a lot of repeated code when dealing with databases. It is also being updated regularly and the maintainer responds to queries very quickly.

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

Sidebar

Related Questions

What would be considered best practice in this case. I've got a class that's
I want to know which of these is considered best practice for iOS development:
Is it considered best practice to use file appenders or database appenders in log4net?
What is considered best practice when referring to a program's name? I've seen: #define
what is considered best practice in the following snippet: int foo(struct data *bar, struct
What is considered best practice for animating view transitions on the iPhone? For example,
When using Spring, what is considered best practice when both JSON and XML is
I'm using VB's StringBuilder, and I was curious what is considered best practice for
Not sure if this is considered best practice or if you should even do
In an ActiveRecord model is it considered best practice/necessary to use validates_presence_of when also

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.