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

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 25, 20262026-05-25T23:26:36+00:00 2026-05-25T23:26:36+00:00

In my Android application; I have a single database with multiple tables. Each table

  • 0

In my Android application; I have a single database with multiple tables.
Each table is more-or-less separate from each other, but figured (for best practice?) to just have one DB file.

When it comes to Upgrades, it’s currently an all or nothing affair. On upgrade, it “DROP”‘s all the tables and re-creates them. However, this is rather harsh if only one of the tables has changed as all the other tables’ data is also lost.

Is there a built-in way to auto-upgrade just the tables that have changed?
(e.g. using a version number per/table?)

If not, I guess I can see two options:

  1. Use separate databases/files for each table, to use built-in version upgrade functionality.

  2. Use the database version number to know when the “schema” has changed, but have a separate table to store the current TABLE_VERSIONS and manage my own upgrade by checking the version number of each table against the current build and DROP/CREATE Tables where needed.

(I’d rather not re-invent the wheel here, so I’m hoping I’m missing something simple…)

  • 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-05-25T23:26:37+00:00Added an answer on May 25, 2026 at 11:26 pm

    You need an abstract class that implements the upgrade process described here. Then you extend this abstract class for each of your tables. In your abstract class you must store you tables in a way(list, hardcoded) so when the onUpgrade fires you iterate over the table items and for each table item you do the described steps. They will be self upgraded, keeping all their existing details. Please note that the onUpgrade event fires only once per database, that’s why you need to iterate over all your tables to do the upgrade of all of them. You maintain only 1 version number over all the database.

    • beginTransaction
    • run a table creation with if not exists (we are doing an upgrade, so the table might not exists yet, it will fail alter and drop)
    • put in a list the existing columns List<String> columns = DBUtils.GetColumns(db, TableName);
    • backup table (ALTER table " + TableName + " RENAME TO 'temp_" + TableName)
    • create new table (the newest table creation schema)
    • get the intersection with the new columns, this time columns taken from the upgraded table (columns.retainAll(DBUtils.GetColumns(db, TableName));)
    • restore data (String cols = StringUtils.join(columns, ",");
      db.execSQL(String.format(
      "INSERT INTO %s (%s) SELECT %s from temp_%s",
      TableName, cols, cols, TableName));
      )
    • remove backup table (DROP table 'temp_" + TableName)
    • setTransactionSuccessful

    (This doesn’t handle table downgrade, if you rename a column, you don’t get the existing data transfered as the column names do not match).

    .

    public static List<String> GetColumns(SQLiteDatabase db, String tableName) {
        List<String> ar = null;
        Cursor c = null;
        try {
            c = db.rawQuery("select * from " + tableName + " limit 1", null);
            if (c != null) {
                ar = new ArrayList<String>(Arrays.asList(c.getColumnNames()));
            }
        } catch (Exception e) {
            Log.v(tableName, e.getMessage(), e);
            e.printStackTrace();
        } finally {
            if (c != null)
                c.close();
        }
        return ar;
    }
    
    public static String join(List<String> list, String delim) {
        StringBuilder buf = new StringBuilder();
        int num = list.size();
        for (int i = 0; i < num; i++) {
            if (i != 0)
                buf.append(delim);
            buf.append((String) list.get(i));
        }
        return buf.toString();
    } 
    
    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

I am developing an android database application in which i have to add multiple
I have a bunch of EditTexts in my Android application, each with InputMethod set
I have an interesting problem being reported to me from an android application I
I have developed an Android application that has 1 process and 2 services. But
I have built an android application which has a single mapview. I have a
I am building an android application that reads text files. Now,i have multiple text
I have a single Android application that houses a suite of applications. I want
I have an Android application with version 1.6 I take the wallpaper and show
I have an Android application that connects to Facebook to request authorization of an
I have two threads in an Android application, one is the view thread, and

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.