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

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: June 17, 20262026-06-17T16:44:12+00:00 2026-06-17T16:44:12+00:00

let’s assume I have a database table test_table with 2 columns and a corresponding

  • 0

let’s assume I have a database table test_table with 2 columns and a corresponding create script in the SQLiteOpenHelper:

DB_VERSION = 1:
public void onCreate(SQLiteDatabase db)
{
db.execSql("CREATE table test_table (COL_A, COL_B);
}

This is the initial app version 1, which is published in the Play Store.

After a while there’s an update to the app and the utilized database.
I guess the SQLiteOpenHelper class has to be adapted like this:

DB_VERSION = 2:
public void onCreate(SQLiteDatabase db)
{
db.execSql("CREATE table test_table (COL_A, COL_B, COL_C)");
}

public void onUpgrade(SQLiteDatabase db, int oldVersion, int newVersion)
{
db.execSql("ALTER TABLE test_table ADD Column COL_C");
}

After some time, another app update:

DB_VERSION = 3:
public void onCreate(SQLiteDatabase db)
{
db.execSql("CREATE table test_table (COL_A, COL_B, COL_C, COL_D)");
}

public void onUpgrade(SQLiteDatabase db, int oldVersion, int newVersion)
{
db.execSql("ALTER TABLE test_table ADD Column COL_D");
}

–> This is where I need advice.
If the user installs app version 1, he has Columns A and B.
If he then updates to version 2, onUpgrade fires and adds a column C.
New users who install from scratch get the 3 columns via the create statement.
If the user then updates to version 3, onUpgrade fires again and a column D is added.
But WHAT IF the user installs app version 1, then skips the update of version 2 and updates version 3? Then he would have missed the

public void onUpgrade(SQLiteDatabase db, int oldVersion, int newVersion)

    {
    db.execSql("ALTER TABLE test_table ADD Column COL_C");
    }

part and only

public void onUpgrade(SQLiteDatabase db, int oldVersion, int newVersion)

    {
    db.execSql("ALTER TABLE test_table ADD Column COL_D");
    }

would be called, which leads to a table test_table(COL_A, COL_B, COL_D)??

What’s the correct way of handling database upgrades of a live app, so the user doesn’t lose his data?
Do you have to check all possible (old) versions in the onUpgrade() method and execute different alter table statements based on that version?

I am asking because in my app, the user has the possibility to export and import the data, which is nothing more than export: copy the whole database away and import: replace the app database with the backup copy database.

What happens if the user has app version 1, exports the database, upgrades the app (new database structure) and imports the old version 1 backup?
–> How will SQLiteOpenHelper behave?
–> What is the correct way to handle db upgrades together with import/export functionality?

  • 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-17T16:44:14+00:00Added an answer on June 17, 2026 at 4:44 pm

    What’s the correct way of handling database upgrades of a live app, so the user doesn’t lose his data? Do you have to check all possible (old) versions in the onUpgrade() method and execute different alter table statements based on that version?

    By and large, yes.

    A common approach to this is to do pair-wise upgrades:

    public void onUpgrade(SQLiteDatabase db, int oldVersion, int newVersion) {
      if (oldVersion<2) {
        // do upgrade from 1 to 2
      }
    
      if (oldVersion<3) {
        // do upgrade from 2 to 3, which will also cover 1->3,
        // since you just upgraded 1->2
      }
    
      // and so on
    }
    

    This roughly equates to Rails migrations, for example.

    What happens if the user has app version 1, exports the database, upgrades the app (new database structure) and imports the old version 1 backup? –> How will SQLiteOpenHelper behave?

    If by “copy the whole database away”, you literally mean a full file copy of the SQLite database file, then when SQLiteOpenHelper goes to open the restored backup, it will that the database has the old schema version and will go through onUpgrade() as normal.

    What is the correct way to handle db upgrades together with import/export functionality?

    I suspect the answer is: either make your backup by copying the entire file, or also arrange to backup and restore the schema version, which you can get by calling getVersion() on a SQLiteDatabase object. That being said, I haven’t dealt with this scenario much, and there may be more issues that I am not thinking of.

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

Sidebar

Related Questions

Let's say that I have a SQLite database that I create in a separate
Let's assume I have the following typedef: typedef void (^myBlock)(id); And I have some
Let's say I have a table with a Color column. Color can have various
Let's say I have such MYSQL table for logins: id---name---location---login_date ---------------------------------- 1----mike----usa-----21.08.2012 2----tony----uk------22.08.2012 3----tony----uk------23.08.2012
Let's say I have some data in a CouchDB database. The overall size is
Let's say that I have classes like this: public class Parent { public int
Let's say I have 2 functions: void function1(int *ptr) { printf(%d, *ptr); } and
Let's say I have the following function in C#: void ProcessResults() { using (FormProgress
Let's say I'm outputting a post title and in our database, it's Hello Y&#8217;all
Let's say I have my own website, my own database and I want to

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.