shared preference and database problem when we update old to new version - android

When we add new shared preference key value details for new version android app and When we add column to existing sqlite database in android will it automatically creates these all details when old user updates from play-store?

If you are installing the new version of existing application Shared Preferences will give no error but in SQLite, you need to change the version of DB also otherwise new changes will not reflect back until you re-install it again, but it will truncate all the existing data in SQLite. So it is better to change the version.
Version is mentioned inside the constructor of your Database class.
For example,
public DatabaseHelper(Context context) {
super(context, DATABASE_NAME, null, 6);
mContext = context;
}
In this the 4th paramter is version

Shared preferences persist until you completely un-install the application.
And as far as database is concerned, you can make changes in it. There is a concept known as "database migration".
If you are using Room, this might help,
https://developer.android.com/training/data-storage/room/migrating-db-versions
If you are using simple Sqlite without Room, you have to make changes in the onUpgrade() function,
#Override
public void onUpgrade(SQLiteDatabase db, int oldVersion, int newVersion) {
// add Column here
if (newVersion > oldVersion) {
db.execSQL("ALTER TABLE foo ADD COLUMN new_column INTEGER DEFAULT 0");
}
}

Related

Managed SQLite Upgrade

Android Developers have to use SQL lite for Android application, that is easy, but the problem comes when we have to upgrade the database version, we have to remove old database and create new database on upgrade, so if we want to add just one column, we have to remove all user data, is there any component or source code that manage the database upgrade, so if it only need one column, just add one column, not delete all tables.
This is completely wrong
when we have to upgrade the database version, we have to remove old database and create new database on upgrade
In your onUpgrade method, it would look something like this:
#Override
public void onUpgrade(SQLiteDatabase db, int oldVersion, int newVersion) {
String upgradeQuery = "ALTER TABLE yourtable ADD COLUMN yourcolumn TEXT";
if (oldVersion == 1 && newVersion == 2)
db.execSQL(upgradeQuery);
}

Dropping SQLite database on upgrading application

I implemented an Android application which use Sqlite database.
When I release a new version of my application (not in playStore), I upload it on my server, so, if the old application is running, calling web service, can understand that new version is available. So, new version is downloaded and installed.
When the application is overinstalled, the database is not dropped, so if I need to do any changes of my database I need to use the method:
public void onUpgrade(SQLiteDatabase _db, int oldVersion, int newVersion)
This is fine, unless I need to do many changes in my database. In this case, the code becomes unreadable. So I would to delete the database and create a new one.
How can I perform this task?
EDIT: What about using context.deleteDatabase(DATABASE_NAME); ?
For throw-away databases (where the data is e.g. a cached copy of data available in the cloud) I usually make onUpgrade() just call onCreate() and make onCreate() execute DROP TABLE IF EXISTS <tablename> before creating the tables.
For example:
#Override
public void onUpgrade(SQLiteDatabase db, int oldVersion, int newVersion) {
onCreate(db);
}
#Override
public void onCreate(SQLiteDatabase db) {
db.execSQL("DROP TABLE IF EXISTS foo");
db.execSQL("CREATE TABLE foo(bar INTEGER, baz TEXT");
}
you can think about this one
keep a trace of your upgrade using a flag in shared preference. when you are downloading new version then set the flag to true. on every launch check the flag. if the flag is true then recreate the database and set the flag false.
I havn't tried similar things but I think it should work in your case.
and to delete database context.deleteDatabase(DATABASE_NAME);
If you just want to re-create your whole database, just drop every table in the old one.

Android app crashes when made some changes in database

I have developed an android app which consists of database.When i install it on mobile it works fine for first time. when i do some changes in database and install it again without uninstalling the previously installed version, it crashes.
Note:I don't want to uninstall my previous version because i want to retain previously stored data
Any suggestions friends?
Increase your database version number by 1 when you change database structure. (i.e adding new column, removing existed column, changing datatype of column, changing column name etc...).
And put below two lines inside onUpgrade() method of your db helper class.
db.execSQL("DROP TABLE IF EXISTS your_table_name_goes_here");
onCreate(db);
Everytime it checks the database current version number. If it finds any mismatch between current database version and earlier version then it simply executes onUpgrade() method to upgrade database. So, we drop all our table(s) and create a new table(s) again.
So now you can add/remove 'n' number of columns while working with db. What all you have to do is just increase the version number by 1 and execute when you change db structure.
When you create a database you will probably use one if these contructors:
public TimePointsSQLiteHelper(Context context, String name,
CursorFactory factory, int version) {
super(context, dbName, factory, dbVersion);
}
private TimePointsSQLiteHelper(Context context) {
super(context, dbName, null, dbVersion);
}
As you can see, there is a variable dbVersion, this is the one where you indicate the version of the database to avoid the problem you are having. As a suggestion use a final static int variable to old the version/number of your database so you can change it easily.
E.g.:
public final static int dbVersion = 1;
Then you should overrite onUpgrade(...) and/or onDowngrade(...) in order to apply the changes to your current app and data.
The simplest and fastest way to do it is the one #SANTHOSH mentioned, just drop all the tables and create them again:
db.execSQL("DROP TABLE IF EXISTS your_table_name");
onCreate(db);
beware that with this solution you will lose your current data saved in your database. You should add more logic to onUpgrade and onDowngrade to migrate the data from one version of the database to the other if you want to keep it.
If you made changes such as adding a column to it you need to either do a full uninstall or upgrade the database version.
Either way you will lose all data in the table unless you make a backup of the data first by making a temp database to store it in

Does creating a table after the database has already been created change the database version in SQLite?

I created an SQLite database for an Android application. The database originally had several tables.
On a particular application version, I added code that created a new table (i.e., db.execSQL("create table if not exists " + table_name ...) but never changed the database version from 1 on the SQLiteOpenHelper's constructor.
Now I want to upgrade the database. My question is:
Would the existing modified database still be version 1 or did the database version number increment automatically when the new table was created (even though the database was not upgraded via the onUpgrade(...) method)?
Thanks in advance.
When you extend your SQLiteOpenHelper class you define which database version to use. For instance, in the following way:
private static class DbWordsHelper extends SQLiteOpenHelper {
private DbWordsHelper(Context context) {
super(context, DB_NAME, null, DB_VERSION);
}
If you upgrade your database you should change DB_VERSION manually in your code and rebuild your application. New version has to be higher then previous one. When SQLiteOpenHelper detects that your version number has been changed it calls onUpgrade method where you should provide logic how to update your database:
#Override
public void onUpgrade(SQLiteDatabase db, int oldVersion, int newVersion) {
// Here in the future should be method that change the schema of the
// database. Now we just delete
try {
db.execSQL(DROP_TABLE_WORDS);
} catch (SQLException e) {
Log.e(TAG, "Error while updating database" + TABLE_WORDS, e);
}
onCreate(db);
}
Thus, even if you add new tables the version is not changed until you manually do this in the code.
It is database version, not table version. So, dropping/recreating table won't effect database version.
database version is what you assigned in the SQLiteOPenHelper, now you have 2 different structured database with same version number, that's really BAD, and onUpgrade will never be called because the same version number.

Does an Android database get completely removed and recreated then updating an app?

I have an app that uses a database. At startup I check to see if my tables are missing and if so I create them. Works great.
I've noticed that if I "adb uninstall" then the next time I run my app the tables are created again (this is what I need), but what about when updating through the marketplace?
I'm in the process of releasing an update, and I've made major changes to the tables. I'd like it if the tables were completely wiped and re-created, in fact my app needs this to happen. If someone updates and has the old tables then there will be a force close.
Does anyone know the specifics of this scenario?
My tables are only for lookup, I store no user data so I dont really care about losing data on the updates.
Of course I know an option is to use some type of "version" table and check to see if I need to manually drop and create, but I was wondering if that was even needed.
Thanks.
On an update the tables are left alone. I'm going to assume you have implemented a subclass of SQLiteOpenHelper to explain this. The way Android is able to handle version changes is through the use of onUpgrade in the SQLiteOpenHelper class. This method is called from SQLiteOpenHelpers constructor if the DB version is older than the version the app expects. From inside onUpgrade is where you are going to drop your old tables and create the new ones.
onUpgrade (SQLiteDatabase db, int oldVersion, int newVersion){
Log.w(TAG, "Upgrading database from version " + oldVersion + " to "
+ newVersion + ", which will destroy all old data");
//we can change this to perform different behaviors
db.execSQL("DROP TABLE IF EXISTS "+MYDB.TABLE_NAME);
onCreate(db);
}
It is important to note the way Android tells if you are using a new DB version.
DatabaseHelper(Context context) {
super(context, DATABASE_NAME, null, DATABASE_VERSION);
}
That field DATABASE_VERSION is used, so you should use a final static int member as part of your DB implementation.
Hope that helps, leave me a comment if you have questions.

Categories

Resources