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

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: June 2, 20262026-06-02T16:20:06+00:00 2026-06-02T16:20:06+00:00

I’m developing an app and using SQLite, contentResolver and contentProvider. Application description My app

  • 0

I’m developing an app and using SQLite, contentResolver and contentProvider.

Application description
My app searches for contacts in internal SQLite DB. the data arrives from external file that is selected on first lunch or when the user press the update option in the menu.

All access to the DB are done using getContentResolver().

ContentProvider
I have a ContactsContentProvider class that extends ContentProvider and holds a reference to ContactsDBAdapter which is my database adapter that extends SQLiteOpenHelper.
(I hope you are all with me until now ).

Problem description
When a user press the update button I want the DB to drop all tables and load the new data (this is done by my file chooser and works great).
in order for the onUpgrade() in my ContactsDBAdapter to work the content provider onCreate() must be called with a higher version then what it had before

@Override
public boolean onCreate() {
      context = getContext();
      pref = PreferenceManager.getDefaultSharedPreferences(context);
      int dbVersion = pref.getInt(Settings.DB_VERSION, 1);
      mDb = new ContactsDBAdapter(context,dbVersion);
      return (mDb == null)? false : true;
}

But I get the contentProvider from my contentResolver so it is not created twice.

Although there are a lot of explanation of how to use both contentProvider and contentResolver I didn’t find anywhere a good upgrade progress.

I’m aware of how the onUpgrade works and that it is being checked during getReadableDatabase() and getWritableDatabase() calls but the fact is that the version will not be diferent since the ContactsDBAdapter is the same instance as it previously was.

I thought about some work arounds but didn’t like them at all.
I can manually check during insert() if the version is higher (but that would be expensive since it is done by every call) and if the answer is true then manually call onUpgrade.

or to try and unregister the Provider in some way but didn’t find any valid and good solution so far.

What is the best practice to upgrade your DB ?

Thanks!

  • 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-02T16:20:07+00:00Added an answer on June 2, 2026 at 4:20 pm

    I found a nice solution.
    In my ContactsContentProvider which extends ContentProvider I added a sharedPrefencesListener

        @Override
    public boolean onCreate() {
          context = getContext();
          pref = PreferenceManager.getDefaultSharedPreferences(context);
          int dbVersion = pref.getInt(Settings.DB_VERSION, 1);
          mDb = new ContactsDBAdapter(context,dbVersion);
          pref.registerOnSharedPreferenceChangeListener(sharedPrefListener) ;
          return (mDb == null)? false : true;
    }
    SharedPreferences.OnSharedPreferenceChangeListener sharedPrefListener = 
      new SharedPreferences.OnSharedPreferenceChangeListener() {
    
        @Override
    public void onSharedPreferenceChanged(SharedPreferences sharedPreferences, 
         String key) {
       if(key.equals(Settings.DB_VERSION)){
        int dbVersion = sharedPreferences.getInt(Settings.DB_VERSION, 1);
        synchronized (mDb) {
                mDb.close();//Not sure this is the right behavior
            mDb = new ContactsDBAdapter(context,dbVersion);
        }
       }
    }
    };
    

    Now when the user changes the version number in my main activity then I set the version number. This will call a new ContactsDBAdapter which will then invoke onUpgrade the next time someone will want getReadableDatabase() or getWriteableDatabase().

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

Sidebar

Related Questions

We're building an app, our first using Rails 3, and we're having to build
I am using Paperclip to handle profile photo uploads in my app. They upload
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
I have a string like this: La Torre Eiffel paragonata all’Everest What PHP function
I am reading a book about Javascript and jQuery and using one of the
I'm using v2.0 of ClassTextile.php, with the following call: $testimonial_text = $textile->TextileRestricted($_POST['testimonial']); ... and
I'm parsing an RSS feed that has an ’ in it. SimpleXML turns this
We are using XSLT to translate a RIXML file to XML. Our RIXML contains

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.