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

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: June 12, 20262026-06-12T09:37:42+00:00 2026-06-12T09:37:42+00:00

[UPDATE: SOLUTION = Fixed onUpgrade() code which allowed successful testing of database schema changes

  • 0

[UPDATE: SOLUTION = Fixed onUpgrade() code which allowed successful testing of database schema changes using Debugger instead of packaging multiple apks]

I am attempting to test 2 apks to insure the database updates will occur when my app update is deployed to Market.

Test1.apk: contains sqlite schema_version = 1
Test2.apk: contains sqlite schema_version = 2

Both apks are digitally signed with the same key, using adb I’m able to install Test1.apk. When I attempt to install Test2.apk, I receive:

Failure [INSTALL_FAILED_ALREADY_EXISTS]

How do I test my new schema changes? If the answer is from the Debugger, then when onUpgrade() is called, my databases is Always LOCKED.

//////////////// This Code Fixed The onUpgrade() Issue /////////////////////

@Override
        public void onUpgrade(SQLiteDatabase db, int oldVersion, int newVersion) { 
            //TODO: Write DB Update logic.

            SQLiteDatabase upgradeDB = null;

            if (newVersion > oldVersion){           
                Log.e(TAG, "NEWER VERSION DETECTED, DATABASE UPGRADE REQUIRED!");               

                InputStream inputStream = myContext.getResources().openRawResource(R.raw.dbscript_v2_0_0_0); 
                BufferedReader br2 = new BufferedReader(new InputStreamReader(inputStream), 1016688); //1016688 max 
                String buffer;
                try {

                    db.beginTransaction();

                    //openDataBase();
                    while ((buffer = br2.readLine()) != null) 
                    { 
                            String[] execSql = buffer.split("\n");  
                            execMultipleSQL(db, execSql);

                    } 
                    db.setTransactionSuccessful();      

                    Log.d(TAG, "onCreated sql: CREATED TABLES and INSERTED RECORDS");
                } catch (SQLException e) {
                    Log.e("Error creating tables and debug data", e.toString());
                } catch (IOException e) {
                    // TODO Auto-generated catch block
                    e.printStackTrace();
                } finally {
                    db.endTransaction();
                    //db.close();
                }

            }
            else
            {
                Log.e(TAG, "NO DATABASE UPGRADE DETECTED"); 
            }
        }

////////////////////////////////////////////////////////////////////////////


        @Override
                public void onUpgrade(SQLiteDatabase db, int oldVersion, int newVersion) { 
                    //TODO: Write DB Update logic.

                    SQLiteDatabase upgradeDB = null;

                    if (newVersion > oldVersion){           
                        Log.e(TAG, "NEWER VERSION DETECTED, DATABASE UPGRADE REQUIRED!");               

                        InputStream inputStream = myContext.getResources().openRawResource(R.raw.dbscript_v2_0_0_0); 
                        BufferedReader br2 = new BufferedReader(new InputStreamReader(inputStream), 1016688); //1016688 max 
                        String buffer;
                        try {

                            String myPath = DATABASE_PATH + DATABASE_NAME;
                            myDataBase = SQLiteDatabase.openOrCreateDatabase(DATABASE_PATH + DATABASE_NAME, null);
                            myDataBase.beginTransaction();

                            //openDataBase();
                            while ((buffer = br2.readLine()) != null) 
                            { 
                                    String[] execSql = buffer.split("\n");  
                                    execMultipleSQL(myDataBase, execSql);

                            } 
                            myDataBase.setTransactionSuccessful();      
                                //close();                  
                                Log.d(TAG, "onCreated sql: CREATED TABLES and INSERTED RECORDS");
                        } catch (SQLException e) {
                            Log.e("Error creating tables and debug data", e.toString());
                        } catch (IOException e) {
                            // TODO Auto-generated catch block
                            e.printStackTrace();
                        } finally {
                            myDataBase.endTransaction();
                            myDataBase.close();
                        }

                    }
                    else
                    {
                        Log.e(TAG, "NO DATABASE UPGRADE DETECTED"); 
                    }
                }


     public void openDataBase() throws SQLException {

                try
                {   
                    /* When the database is 1st installed on the device, this helper class creates a blank database using getWritableDatabase().
                     * The actual database is then populated using the SQLiteDatabase.openDatabase(PATH, null, Open_ReadWrite) to exec the sql.
                     * 
                     * PROBLEM - SQLiteDatabase.openDatabase() Does not call onUpgrade() so we're 
                     * unable to update the database for future versions
                     * 
                     * SOLUTION - Check to see if the dbExists, if not use SQLiteDatabase.openDatabase() 
                     * because it will create the DB from scripts, we don't care about onUpgrade();
                     * If the db does exist, call getWritableDatabase() to invoke onUpgrade checks for 
                     * future releases where the SCHEMA_VERSION is greater than the current db SCHEMA_VERSION
                     */
                    boolean dbExist = checkDataBase();
                    if(!dbExist){

                        /* Calls: DOES NOT call onUpgrade()
                         * Errors if db exists and there are db changes (New Columns/Tables), so use the below getWritableDatabase() instead                     */
                        String myPath = DATABASE_PATH + DATABASE_NAME;
                        myDataBase = SQLiteDatabase.openDatabase(myPath, null, SQLiteDatabase.OPEN_READWRITE);
                    }
                    else
                    {
                        /* Calls: onUpgrade()
                         * Errors if db does not exist, so use the above instead                     */
                        myDataBase = this.getWritableDatabase();                    
                    }               

                }
                catch (Exception e) {
                    Log.e(TAG, "Error SQLiteFactoryAdapter openDataBase " + e.toString());
                }
            }

  • 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-12T09:37:43+00:00Added an answer on June 12, 2026 at 9:37 am

    How are you opening the database?

    It looks like you are extending SQLiteOpenHelper. The constructor has a parameter version, which represents the version of the schema. Why don’t you just change this param in your test to test your onUpgrade() method

    Side note: The reason you are getting the error message is because you are trying to deploy the same package again. You need to uninstall it before deploying it again. You can do this with the command adb uninstall packagename.apk.

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

Sidebar

Related Questions

Update Solution Found See Bottom of post if interested Seems simple enough and for
I am not interested in any auto update solution, such as ClickOnce or the
[UPDATE] the clean goal runs smoothely all over the projects of the solution, the
Update: I've added an answer that describes my final solution (hint: the single Expr
NOTE: I added my new solution at the UPDATE answer below. I try to
Update: This question was an epic failure, but here's the working solution. It's based
Possible Duplicate: SQL ORDER BY total within GROUP BY UPDATE: I've found my solution,
Update: I reported this as a bug to Apple and they fixed it! All
Update: The requirement is fixed length 9 digits so 460 000 000 138 should
Suppose this code using neato: graph sample { layout=neato overlap=false splines=true tailclip=false headclip=false A

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.