add new table onUpgrade android - android

During an upgrade, I want to add a new table to my database, but also not lose the data from the other tables when upgrading the application. Can someone tell me, (but if you could show me some example also) of how this is done. Because I've looked through the forums but mainly there are discussions about adding a new column etc. I figured that I have to do it somehow with alter table, but I did not understand everything. If you can tell me the steps of this process I would really appreciate it. Thank u in advance.

If you just want to add a new table and not modify any of your existing tables, then you could just create the new table in your onUpgrade method. This way your existing tables will be untouched.
EDIT: Even better, add the table as usual in onCreate and then in onUpgrade you call onCreate

Try using SQLiteOpenHelper in Android. It has methods for onCreate and onUpgrade.
Sample:
public void onUpgrade(SQLiteDatabase database, int oldVersion, int newVersion) {
Maas360Logger.i(loggerName, "upgrading database "+oldVersion+" "+newVersion);
try {
database.beginTransaction();
for (int i = oldVersion + 1; i <= newVersion; i++) {
// Future schema changes has to go into this loop
Maintain database versions to handle upgrades

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.

Perform specific operation at the time of installing apk?

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?
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);
}
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.

Upgrade database. Can I alter tables or need to create new?

I'm using a database in my application which is currently on the Google Play. Now I'm releasing an update and have added a new column to an existing table. Looks like I need to use onUpgrade() method of SQLiteOpenHelper class. I'm just wondering if I need to recreate the whole table, which leads to backup/restore (which is complex) process, or I can just alter it on upgrade? How shall I handle upgrade in the code? Is it just enough to implement onUpgrade() method? Might be any good example? Really appreciate your input, thank you very much.
Yes, you can alter table . No need to create it again .
private static final String ALTER_USER_TABLE_ADD_USER_SOCIETY = "ALTER TABLE user_table ADD user_society TEXT";
public void onUpgrade(SQLiteDatabase db, int oldVersion, int newVersion)
{
if(newVersion > oldVersion)
{
db.execSQL(ALTER_USER_TABLE_ADD_USER_SOCIETY);
}
}

how to upgrade new table an existing apps?

Friends,
I have an existing apps published and during an upgrade, I want to add a new table to my database, but also not lose the data from the other tables when upgrading the application. Can someone tell me, (but if you could show me some example also) of how this is done and how to test it. Because I've looked through the forums but mainly there are discussions about adding a new column etc. I figured that I have to do it somehow with alter table, but I did not understand everything. If you can tell me the steps of this process I would really appreciate it.I have used DBAdapter class extends SQLiteOpenHelper.Accept my question and asap give me solution..
Everything happens in your class DBAdapter. There you must implement a method onUpgrade which adds your new table to the database. Here is a skeleton for doing this:
class DBAdapter extends SQLiteOpenHelper {
// Implement the other methods
// This method is called when your application is being upgraded.
public void inUpgrade(SQLiteDatabase db, int oldVersion, int newVersion) {
Log.i(TAG, "adding table to the database") ;
db.execSQL("CREATE TABLE MyNewTable(_id INTEGER, name TEXT)") ;
Log.i(TAG, "upgrade done") ;
}
You may look at the documentation of the class SQLiteOpenHelper

Categories

Resources