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 4058914
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 20, 20262026-05-20T15:08:24+00:00 2026-05-20T15:08:24+00:00

Dear Fellow Android Developers! EDIT: Thank you all for your answers. I see from

  • 0

Dear Fellow Android Developers!

EDIT:

Thank you all for your answers. I see from many of them that it seems to be common (and accepted) practice to write your own close() method in your database adapter. Fair enough.

But how does that work with a ContentProvider? Usually when querying my database through my ContentProvider I simply issue something like:

Cursor managedCursor = managedQuery(...);

I don’t see how I, with this methodology, can access the custom close() method in my custom ContentProvider implementation. Should I instead, from my Activity, do something like:

MyCustomProvider myProvider = (MyCustomProvider) getContentResolver();

and then:

myProvider.query(...);
myProvider.close();

And above all; is this at all necessary (as of point 2 below)?

END EDIT

To a certain degree I must say that I get the concept of the SQLiteOpenHelper, what it is, how it’s used and so. I even use it on a regular basis when I write my own ContentProvider‘s.

The thing is that I’m not sure what to do with the SQLiteDatabase object, returned by the myOpenHelper.getWritableDatabase() (or the myOpenHelper.getReadableDatabase() function for what matters) when I’m done with it.

According to Android ContentProvider.onCreate() documentation:

You should defer nontrivial initialization (such as opening, upgrading, and scanning databases) until the content provider is used (via query(Uri, String[], String, String[], String), insert(Uri, ContentValues), etc).

[…]

If you do use SQLiteOpenHelper, make sure to avoid calling getReadableDatabase() or getWritableDatabase() from this method. (Instead, override onOpen(SQLiteDatabase) to initialize the database when it is first opened.)

The above gives me a hint where to initialize the database (the query(...), insert(...), etc functions), but it doesn’t tell me anything on how to treat the created SQLiteDatbase object when I’ve finished using it.

  1. Should I save it as a member variable of my ContentProvider implementation (and treat it much like a “private singleton” for future use)?

  2. Should I just leave it when exiting the query(...), insert(...), etc. functions and trust that the SQLiteOpenHelper will manage it for me in future calls?

  3. [Insert your alternative point-of-view here]

Being the confiding (or lazy) developer I’ve implemented my code according to the second alternative above. But I can’t get rid of the creepy feeling that I’m neglecting something important.

  • 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-20T15:08:25+00:00Added an answer on May 20, 2026 at 3:08 pm

    It depends on what you’re doing with your database. If you just do an insert, delete or select where you get an business object back, then you can close the database right after using it. As far as I know it is designed that you simply close it and request a new one when ever you need it.

    But be careful when you’re working with a cursor then you have to keep the database open as long as the cursor is in use. Otherwise the application will crash when the cursor has to reload data.

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

Sidebar

Related Questions

Dear all: In advance, thank you for your time. Lately, I have decided to
Dear all, I am writing a python program that is retrieving edifact log messages
dear all, i'll take some data from the database. and join two tables.the code
I've just found out that a spammer is sending email from our domain name,
Dear all, I save formatted text (bold, changed in font, style...etc) in an nvarchar(max)
Dear all, I have a question about Facebook Page: ( NOT user profile page,
dear all,this is my code at xampp: $sql = INSERT INTO oqc_defect ; $sql.=
dear all.can you tell me how to collecting this data in mysql: item colour
dear all..i'm newbie at time picker. can you tell me how to use jquery
dear all.i newbie at web programming and until now i still have learn about

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.