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

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: June 5, 20262026-06-05T13:36:08+00:00 2026-06-05T13:36:08+00:00

In my app, I am supplying the most current version of my database via

  • 0

In my app, I am supplying the most current version of my database via the assets folder. For simplicity’s sake, lets say my DB scheme is PRIM KEY INTEGER _id, TEXT name and INTEGER quantity. Quantity is the only thing that the user can edit in the app, everything else is static data. Quantity defaults at 0.

Here’s what I’m currently trying to do onUpgrade:

1.) Iterate through all of the tables, find the entries that have a quantity > 0, and store those entries in a list of objects that hold the ID and Quantity values, as well as the originating table name.

2.) Delete the old database that I just iterated through

3.) Copy the new database from assets that contains updated static information.

4.) Iterate through the list from step 1, and update the corresponding IDs with the quantities in the corresponding tables.

Here is my onUpdate method (titleArray is an array of strings containing my table names):

@Override
public void onUpgrade(SQLiteDatabase db, int oldVersion, int newVersion) {

    Log.w("TaskDBAdapter", "Upgrading from version " + oldVersion + " to " + newVersion);


    ArrayList<backupKey> bak = new ArrayList<backupKey>();

    Cursor cursor = null;
        for(int i = 0; i < titleArray.length; i++)
        {
             cursor = db.rawQuery("SELECT * FROM " + titleArray[i] + " WHERE quantity > 0", null);
             if (cursor.moveToFirst()){
                   do{
                       backupKey key = new backupKey(titleArray[i],
                               cursor.getInt(cursor.getColumnIndex("_id")),
                               cursor.getInt(cursor.getColumnIndex("quantity")));
                       bak.add(key);

                   }while(cursor.moveToNext());
                }

        }
        cursor.close();

        myContext.deleteDatabase(DB_NAME);

        try 
        {
            InputStream myInput = myContext.getAssets().open(DB_NAME);

            String outFileName = DB_PATH + DB_NAME;

            OutputStream myOutput = new FileOutputStream(outFileName, false);


            byte[] buffer = new byte[1024];
            int length;
            while ((length = myInput.read(buffer))>0){
                myOutput.write(buffer, 0, length);
            }

            myOutput.flush();
            myOutput.close();
            myInput.close();

        } catch (IOException e) 
        {
            Log.w("TaskDBAdapter", "failed");
            throw new Error("Error copying database");

        }

    for (int i = 0; i < bak.size(); i++)
    {

        ContentValues args = new ContentValues();
        args.put("quantity", bak.get(i).quantity);
        String where = "_id = ?";
        String[] whereArgs = {Integer.toString(bak.get(i).id)};
        db.update(bak.get(i).range, args, where, whereArgs);
    }

} //End of onUpgrade

I can confirm that the arraylist is successfully populated with the proper data. I was trying to just overwrite the database before, but it never seemed to work. I had to add myContext.deleteDatabase(DB_NAME); before the file writing operation for the database to be successfully replaced. I can confirm that my final update iteration isn’t generating any weird data– it has the correct values for the table name, id, and quantity.

The problem is, the quantities are not updated after my onUpgrade. The static data is updated to the latest version I have included in my assets folder, but the quantity data isn’t updated. Is this because I destroyed the database originally supplied to the method (the “db” parameter, in this case)? Is that even what the parameter is? How can I get the data updated in the new database?

Also: If there is a better way to go about this, I’m all ears =)

  • 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-05T13:36:10+00:00Added an answer on June 5, 2026 at 1:36 pm

    onUpgarde() is meant for updating database structure (adding/renaming columns, etc), not the actual data. You might want to do this in the actual app when starting for the first time. Additionally trying to upgrade user-entered data in place might not be the best idea. Why not have two tables (or databases even) — one with the data you are supplying and one with user entered data? You can simply copy rows users edit, and leave the original data read-only.

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

Sidebar

Related Questions

My app was built with SDK 2.2.1 version even before 3.0 beta appear. User
I want to search the current location in my app, but I see entire
My app basically lets me go in and copy an image to a clipboard.
'app name' specifies a minimum os of version 4.3, which is too high to
My app is being launched via a URL and I've added the following method
Let say I want to create comic reader app where the user can download
After successfully building the distribution version of my app for submission to iTunes Connect,
App.config: <?xml version=1.0 encoding=utf-8 ?> <configuration> <appSettings> <add key=1 value=An error occured.\r\nPlease try again
App Engine only allows you to use these formats for XMPP addresses: app-id@appspot.com anything@app-id.appspotchat.com
app.get('/', function (req, res) { res.render('home.jade', { results: req.session.value }); }); What i would

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.