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

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 28, 20262026-05-28T06:23:31+00:00 2026-05-28T06:23:31+00:00

When my app is created, my database is copied from the assets folder to

  • 0

When my app is created, my database is copied from the assets folder to my app.

Now if i want to do an onUpgrade, i just want to overwrite all Tables EXCEPT of one.

But how can i do this?
I just can write the whole database or nothing…

Please help me with this.

This doesnt work of course, because it doesnt overwrite the existing tables, it just backups the one i do not replace..

public void createDataBase() throws IOException{
        boolean dbExist = checkDataBase();
        if(dbExist){   
            myDataBase = this.getWritableDatabase();
        } else {
            myDataBase = this.getReadableDatabase(); 
            try { 
                copyDataBase(); 
            } catch (IOException e) { 
                throw new Error("Error copying database"); 
            }
        }       
    }
private void copyDataBase() throws IOException{

        //Open your local db as the input stream
        InputStream myInput = myContext.getAssets().open(DB_NAME);

        // Path to the just created empty db
        String outFileName = DB_PATH + DB_NAME;

        //Open the empty db as the output stream
        OutputStream myOutput = new FileOutputStream(outFileName);

        //transfer bytes from the inputfile to the outputfile
        byte[] buffer = new byte[1024];
        int length;
        while ((length = myInput.read(buffer))>0){
            myOutput.write(buffer, 0, length);
        }

        //Close the streams
        myOutput.flush();
        myOutput.close();
        myInput.close();

    }
@Override
    public void onUpgrade(SQLiteDatabase db, int oldVersion, int newVersion) {
        Log.i("onUpgrade", "newVersion: "+newVersion+", oldVersion: "+oldVersion);
        try{
        if(newVersion > oldVersion){
            db.execSQL("ALTER TABLE Results RENAME TO temp_Results");
            db.execSQL("CREATE TABLE Results (lektionenId INTEGER PRIMARY KEY, testPassed Integer, lektionPassed Integer)");
            db.execSQL("INSERT INTO Results (testPassed, lektionPassed) SELECT testPassed, lektionPassed FROM temp_Results");
            db.execSQL("DROP TABLE IF EXISTS temp_Results");
        }
        } catch(Exception e){
            Log.i("exc", ""+e);
        }
    }

EDIT:

Where i call the Database:

myDbHelper = new LernAppOpenHelper(this, "LernApp", DbConfig.DB_VERSION_LERNAPP);       

        try {
            String[] columns = {"_id","description"};
            myDbHelper.createDataBase();
            myDbHelper.openDataBase();

            cursor = myDbHelper.getQuery("Uebersicht", columns, null, null, null, null, null);

And my onUpgrade Method:

@Override
    public void onUpgrade(SQLiteDatabase db, int oldVersion, int newVersion) {
        if(newVersion > oldVersion){
            if("LernApp".equals(DATABASE_NAME)){
                myContext.deleteDatabase(DATABASE_NAME);
                try { 
                    copyDataBase(); 
                } catch (IOException e) { 
                    throw new Error("Error copying database"); 
                }
            }

        }

    }
  • 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-28T06:23:31+00:00Added an answer on May 28, 2026 at 6:23 am

    Step #1: Copy your table data from the old database into memory.

    Step #2: Restore your database from your copy in assets.

    Step #3: Update your new database from the table data in memory.

    If the table might be too big for that, copy the data to a file and then back from a file.


    EDIT

    Or:

    Step #1: Copy your old database (the one being replaced) to a new database using standard Java file I/O.

    Step #2: Restore your database from your copy in assets.

    Step #3: Open both databases.

    Step #4: Load the table you are keeping from the old database and pour its contents into the new database. Be sure to watch your transactions: the default of one-transaction-per-statement can make this sort of work very slow.

    Step #5: When done, close the old database and delete it.

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

Sidebar

Related Questions

I want to create a trace on a database server from my C# app,
I have created an app using a badly named database, all alterations to important
I have a database created from a file in my assests folder. When the
I've created a database in Visual Studio 2008 in an App_Data folder of a
I have created a database for my android app which contains static data and
I copied a SQL Server database from one system to the next, identical setup,
So I'm just confused here. I've got to migrate my database from MySQL to
So I got SQLite database working for my app. It loads text values from
Is it possible to protect sqlite database inside mobile app created using PhoneGap +
I have created a database for my app with 5 columns. Each column only

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.