0

I want to add a few columns to my sqlite and maybe new tables to my app, but its already in the market and i know that if i change the something on database structure then the app needs uninstall and reinstall.

Will this happen to a live app and the user will have to uninstall first or will it update sucessfully? Thank you

EDIT: What i had in on upgrade is this:

@Override
    public void onUpgrade(SQLiteDatabase db, int oldVersion, int newVersion) {
        db.execSQL("DROP TABLE IF EXISTS titles");
        onCreate(db);
    }

but i quess it never worked. If i replace it with

@Override

    public void onUpgrade(SQLiteDatabase db, int oldVersion, int newVersion) {
        String query = "ALTER TABLE tablename ADD COLUMN newcolumn INT";
        if (oldVersion == 1 && newVersion == 2)
             db.execSQL(query );
    } 

will it be ok?

4 Answers4

0

You have to handle manipulation inside the onUpgrade method.

@Override
public void onUpgrade(SQLiteDatabase db, int oldVersion, int newVersion) {
    String query = "ALTER TABLE tablename ADD COLUMN newcolumn INT";
    if (oldVersion == 1 && newVersion == 2)
         db.execSQL(query);
}
intrepidkarthi
  • 3,104
  • 8
  • 42
  • 75
0

If I change something on database structure then the app needs uninstall and reinstall?

Why would that be? The SQLiteOpenHelper class provides a good mechanism for handling changes into the structure of an app's database. Namely, onUpgrade(). You can perform whatever changes are necessary (creating new tables, dropping old ones, altering, &c in there).

From the documentation of the constructor:

  • version: number of the database (starting at 1); if the database is older, onUpgrade(SQLiteDatabase, int, int) will be used to upgrade the database; if the database is newer, onDowngrade(SQLiteDatabase, int, int) will be used to downgrade the database
matiash
  • 54,791
  • 16
  • 125
  • 154
  • I have a few questions. When i check for versions the versions automatically increase? So i will check in general if oldVersions>newVersion then upgrade the db right? And one more thing. I will add my columns or tables inside my alter code only right? I wont add them to my main table creation? And how will i know which things i will alter each time if i only add a code to alter a specific table? Like this time i will alter tableA and on the next version i want to alter TableB also, but the code is not there, its only for TableA. Many questions i know:P – antonis lambrianides Jul 11 '14 at 06:07
0

Using a SQLiteOpenHelper this problem is resolved. Using this class you will have a db_version. Everytime you increase your db_version, you have a method called onUpgrade that will be called.

Check this out.

One thing you have to take into consideration is the db_version of your previous app version. You will need to apply all the changes between your old db_version to your last db_version.

Hope it helps

Community
  • 1
  • 1
zozelfelfo
  • 3,776
  • 2
  • 21
  • 35
0

No. If a user is updating an application (not uninstalling it - in that case it will be deleted) and a database already exists then Android will not just delete it (it could contain important information!).

Instead the onUpgrade method of your SQLiteOpenHelper will be called, and it is up to you to decide if you want to clear the data or preserve it.

Laxmeena
  • 780
  • 2
  • 7
  • 28