Pregunta

I have updated the application's database. But users who have updated the application from market will see a crash everytime app is started because new database structure is not compatible with old database. Its not good to ask them to uninstall and install the app, I need to perform an operation just for single time at the time of installation i.e. clear the old database and create new one. This should not be called everytime the app starts, only at the time of installation..or when the app starts for the first time.

I think I have clearly defined my situation, now where do I go from here? Should I bug users to uninstall and install app or its possible to do what I have asked?

¿Fue útil?

Solución

Just change the version of your database and by overriding onUpgrade on your DatabaseHelper class.

    private static final int DATABASE_VERSION = 2;

    @Override
    public void onUpgrade(SQLiteDatabase db, int oldVersion, int newVersion) {
        // TODO Auto-generated method stub
        Log.w(TAG, "Upgrading database from version " + oldVersion + " to " + newVersion
                + ", which will destroy all old data");

        db.execSQL("DROP TABLE IF EXISTS " + TABLE_CREATION);
        onCreate(db);
    }

Otros consejos

Why don't you implement the onUpgrade() method of SQLiteOpenHelper? This class provides useful onCreate() and onUpgrade() methods.

See here : Is the onUpgrade method ever called?

Or here : How to update table schema after an app upgrade on Android?

First of all it is really a bad idea to clear the old database and create a new one (Users will be really pissed seeing there data lost).

You should always try to upgrade the previous data with new columns and stuff. Instead of clearing the whole data, you should always try to alter the structure of tables without clearing the data.

One more thing is that you can upgrade to new database in onUpgrade() method of the DBHelper class.

Licenciado bajo: CC-BY-SA con atribución
No afiliado a StackOverflow
scroll top