Sam
Sam

Reputation: 6395

how to upgrade the android app version one Own SQLite Database without losing the existing data

I need to update my android application in the market to next version.next version i need to update the sqlite DB without losing the exsiting data.

In version one i did not create the tables in runtime instead get the database file from the "assets" folder and copies into the system database path of my application. Refer to this link http://www.reigndesign.com/blog/using-your-own-sqlite-database-in-android-applications/

next version of my app i have modify the exsting table columns and add extra records and there are few new tables as well.I have updated the data base and copied to asset folder.

This will work for users who buy the app for first time,but not for existing users,my question is how can i update the version one users data base without losing existing data

Sam.

Upvotes: 4

Views: 4523

Answers (4)

Soubhab Pathak
Soubhab Pathak

Reputation: 619

In your SQLiteHelper class the DATABASE_VERSION variable should be the latest version. Suppose earlier DATABASE_VERSION was 1 and as you are upgrading so it must be 2.

DatabaseHelper(Context context)
    {
        super(context, DATABASE_NAME, null, DATABASE_VERSION);
    }

Now Upgrade the old database version to new Version. If you don't set the latest version number in the database then onUpgrade(..., ...) will get called repeatedly.

@Override
    public void onUpgrade(SQLiteDatabase db, int oldVersion, int newVersion)
    {
        logger.info("DBManager - :::: Upgrading database from version " + oldVersion+ " to "+ newVersion + ", which will modify some old table");


        String sqlFileName = "update_version_1.sql";

        int totalSQLQueries = executeSQLScript(db, sqlFileName);

        logger.info("DBManager - :::: Upgrading database from version - " +"Total " + totalSQLQueries +" queries executed succesfully from " +sqlFileName);

        if(db.getVersion() == oldVersion) {
            db.setVersion(newVersion);
            logger.info("DBManager - :::: DB Version upgraded from " +oldVersion +" to " +newVersion);
        }
    }

Your database modification code must be written within a transaction. See the code bellow as an example of using transaction for database upgrade :-

    private int executeSQLScript(SQLiteDatabase db, String sqlFileName) 
    {
        int count = 0;

        InputStream inputStream = null; 
        BufferedReader bufferReader = null;

        try
        {
            db.beginTransaction();

            inputStream = localContext.getApplicationContext().getAssets().open(sqlFileName);
            bufferReader = new BufferedReader(new InputStreamReader(inputStream));
            String inputLine;

            while((inputLine = bufferReader.readLine()) != null)
            {
                if(inputLine.trim().equalsIgnoreCase("BEGIN;") || inputLine.trim().equalsIgnoreCase("END;"))
                {
                    continue;
                }
                else
                {
                    db.execSQL(inputLine);
                    count = count + 1;
                }
            }

            db.setTransactionSuccessful();
        }
        catch(Exception ex) {
            ex.printStackTrace();
        }
        finally
        {
            if(bufferReader != null)
            {
                try {
                    bufferReader.close();
                } catch (IOException e) {
                    e.printStackTrace();
                }
            }

            if(inputStream != null)
            {
                try {
                    inputStream.close();
                } catch (IOException e) {
                    e.printStackTrace();
                }
            }

            db.endTransaction();
        }

        return count;
    }

Upvotes: 0

Robert Greathouse
Robert Greathouse

Reputation: 1034

You could try a patch-like solution I describe in this blog post. It will aid with incremental upgrading and will continue to scale as you build more and more versions.

http://www.greenmoonsoftware.com/2012/02/sqlite-schema-migration-in-android/

Upvotes: 1

Chris
Chris

Reputation: 1637

   @Override
    public void onUpgrade(SQLiteDatabase db, int oldVersion, 
    int newVersion) 
    {
        Log.w(TAG, "Upgrading database from version " + oldVersion 
                + " to "
                + newVersion + ", which will destroy all old data");
        if(oldVersion == 2 && newVersion == 3)
        {
            db.execSQL("ALTER TABLE xyz ADD bobby int default 0");    
        }
        else
        {
            db.execSQL("DROP TABLE IF EXISTS xyz");
            onCreate(db);
        }
    }
}    

Upvotes: 3

pawelzieba
pawelzieba

Reputation: 16082

Prepare sql query to upgrade database. If database exists then perform updating else copy database from assets.

In tutorial that you provided is such code:

    if(dbExist){
        //do nothing - database already exist
    }else{

In place where is //do nothing - database already exist put your upgrading code.

Upvotes: 1

Related Questions