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

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: June 9, 20262026-06-09T02:02:36+00:00 2026-06-09T02:02:36+00:00

I am trying to add three new table to my existing sqlite db and

  • 0

I am trying to add three new table to my existing sqlite db and I’m running into problems with the db version not updating after a successful upgrade. Below is the DatabaseHelper that runs:

private static class DatabaseHelper extends SQLiteOpenHelper {

    public DatabaseHelper(Context context, String name, CursorFactory factory, int version) {
        super(context, name, factory, version);
    }

    @Override
    public void onCreate(SQLiteDatabase db) {
        Log.w(TAG, "Current db version is " + db.getVersion());
        db.execSQL(ORIGINAL_DATABASE_CREATE);

    }

    @Override
    public void onUpgrade(SQLiteDatabase db, int oldVersion, int newVersion) {
        Log.w(TAG, "Old Version " + oldVersion + " New Version " + newVersion + " db.getVersion is " + db.getVersion());
        db.execSQL(NEW_TABLE_1_DATABASE_CREATE);
        db.execSQL(NEW_TABLE_2_DATABASE_CREATE);
        db.execSQL(NEW_TABLE_3_DATABASE_CREATE);
        db.setVersion(newVersion);
        Log.w(TAG, "Version after onUpgrade is " + db.getVersion());
    }       
}

Below is my open() function:

public VehicleExpDbAdapter open() throws SQLException {
    mDbHelper = new DatabaseHelper(mCtx, DATABASE_NAME, null, DATABASE_VERSION);
    mDb = mDbHelper.getWritableDatabase();
    return this;
}

Below is my close() function:

public void close() {
    mDb.close();
    mDbHelper.close();
}

So what happens on the first run with a higher DATABASE_VERSION is the first onUpgrade Log reads out:
Old Version 1 New Version 2 db.getVersion is 1

The second Log is:
Version after onUpgrade is 2

But then when the db is accessed again after the onUpgrade was ran the version number of the DB is not updated and it runs through the onUpgrade with the first log in the onUpgrade reading:
Old Version 1 New Version 2 db.getVersion is 1

Then the app crashed because it tries creating a table that is already there.

I’ve tried not manually setting the db version in the onUpgrade as well. This didn’t work either. I’ve also tried updating the version number by running…

db.execSQL("PRAGMA user_version = " + newVersion);

…at the end of the onUpgrade.

EDIT:

Per Aswin Kumar’s suggestion I’ve changed my onUpgrade to backing up my existing table and dropping all table and then recreating them. This has not fix my version issue. Below is my onUpgrade and onCreate:

@Override
    public void onCreate(SQLiteDatabase db) {
        Log.w(TAG, "Current db version is " + db.getVersion());

        db.execSQL(ORIGINAL_DATABASE_CREATE);

        db.execSQL(NEW_TABLE_1_DATABASE_CREATE);
        db.execSQL(NEW_TABLE_2_DATABASE_CREATE);
        db.execSQL(NEW_TABLE_3_DATABASE_CREATE);

    }

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

        Log.w(TAG, "Old Version " + oldVersion + " New Version " + newVersion + " db.getVersion is " + db.getVersion());

        mOldDbContents = backupTables(db);

        db.execSQL("DROP TABLE IF EXISTS " + ORIGINAL_DATABASE_CREATE);
        db.execSQL("DROP TABLE IF EXISTS " + NEW_TABLE_1_DATABASE_CREATE);
        db.execSQL("DROP TABLE IF EXISTS " + NEW_TABLE_2_DATABASE_CREATE);
        db.execSQL("DROP TABLE IF EXISTS " + NEW_TABLE_3_DATABASE_CREATE);

        onCreate(db);
    }   

Below are the sqlite statements that I use to create the tables:

private static final String ORIGINAL_DATABASE_CREATE = "create table " + VEHICLE_EXPENSE_TABLE_NAME + 
        " (" + KEY_ROW_ID + " integer primary key autoincrement, " + 
        KEY_UNIX_DATE + " integer, " + 
        KEY_DATE + " not null default current_date, " +
        KEY_TIME + " not null default current_time, " + 
        KEY_DESCRIPTION + " text, " + 
        KEY_START_MILE + " integer, " + 
        KEY_END_MILE + " integer, " + 
        KEY_MILES + " text, " + 
        KEY_AMOUNT + " text, " 
        + KEY_PARTY_ID + " integer)";

private static final String NEW_TABLE_1_DATABASE_CREATE = "CREATE TABLE " + PURCHASE_HISTORY_TABLE_NAME + 
        "(" + HISTORY_ORDER_ID_COL + " TEXT PRIMARY KEY, " +
        HISTORY_STATE_COL + " INTEGER, " + 
        HISTORY_PRODUCT_ID_COL + " TEXT, " + 
        HISTORY_DEVELOPER_PAYLOAD_COL + " TEXT, " + 
        HISTORY_PURCHASE_TIME_COL + " INTEGER)";

private static final String NEW_TABLE_2_DATABASE_CREATE = "CREATE TABLE " + PURCHASED_ITEMS_TABLE_NAME + 
        "(" + PURCHASED_PRODUCT_ID_COL + " TEXT PRIMARY KEY, " + 
        PURCHASED_QUANTITY_COL + " INTEGER)";

private static final String NEW_TABLE_3_DATABASE_CREATE = "CREATE TABLE " + TRIAL_LIMIT_TABLE_NAME + 
        "(" + TRIAL_PRODUCT_ID_COL + " TEXT PRIMARY KEY, " + 
        TRIAL_PRODUCT_NAME + " TEXT, " + 
        TRIAL_START_DATE + " INTEGER)";

Any help would be great appreciated.

Thank you,
Kevin

  • 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-09T02:02:38+00:00Added an answer on June 9, 2026 at 2:02 am

    Fixed my own problem. The issue was that I had created a separate DB helper class that was accessing the same DB that was was trying to upgrade. In this separate DB helper class the version number was set to 1. This app was my first Java and Android program and I setup the DB classes horribly. I’ve consolidated all the DB class to the one i’m now upgrading and now my DB version is updating like it should. Below are my final classes if anyone is interested.

    @Override
        public void onCreate(SQLiteDatabase db) {
            Log.w(TAG, "Current db version is " + db.getVersion());
    
            db.execSQL(VEHICLE_EXPENSE_DATABASE_CREATE);
    
            db.execSQL(PURCHASE_HISTORY_DATABASE_CREATE);
            db.execSQL(PURCHASE_ITEMS_DATABASE_CREATE);
            db.execSQL(TRIAL_TIME_DATABASE_CREATE);
        }
    
    
    @Override
        public void onUpgrade(SQLiteDatabase db, int oldVersion, int newVersion) {
    
            Log.w(TAG, "Old Version " + oldVersion + " New Version " + newVersion + " db.getVersion is " + db.getVersion());
    
            if (oldVersion < newVersion) {
    
                if (oldVersion == 1) {
                    db = upgradeTo2(db);
                }
            }
        }
    
    private SQLiteDatabase upgradeTo2(SQLiteDatabase db) {
    
            db.beginTransaction();
            try {
                db.execSQL(PURCHASE_HISTORY_DATABASE_CREATE);
                db.execSQL(PURCHASE_ITEMS_DATABASE_CREATE);
                db.execSQL(TRIAL_TIME_DATABASE_CREATE);
                db.setTransactionSuccessful();
            } catch (Exception e) {
                Log.w(TAG, "onUpgrade failed");
            } finally {
                db.endTransaction();
            }
    
            return db;
        }
    
    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

I am trying to do this List<String> stringList = new ArrayList<String>(); stringList.add(one); stringList.add(two); stringList.add(three);
I'm trying to manually add three headers to a table. The table fills out
Hey there, new to AJAX, JS.. Im trying to add to a Javascript/Ajax search
I´m trying to add a list to a table cell but there are no
Im new to asp.net mvc. I'm trying add new model class but it got
I want to add a new column to a table which already consists billions
I'm trying to add a table row with ajax/jquery that has a form element
I am new in eclipselink and trying to add extra columns in manyTomany association
I have a mysql database (version 5.5), and I am trying to add a
I am trying to add an existing PDF (created otherwise) to a PDF created

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.