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

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

Related

How to add variables taken from a previous activity to the SQLite table as columns in android

I have already known how to create a SQL table with specified columns.
But I got this question when new variables(like names) need to be added to an existing SQL table as columns. Please note this is for android studio.
Can you guys give some advice on this?
Thanks a lot!
You can use ALTER TABLE function on your onUpgrade() method in the SQLiteHelper class, to do this.
#Override
public void onUpgrade(SQLiteDatabase db, int oldVersion, int newVersion) {
// If you need to add a column
if (newVersion > oldVersion) {
db.execSQL("ALTER TABLE table_name ADD COLUMN new_column INTEGER DEFAULT 0");
}
}
You may refer this link for syntax Alter Table
Besides, don't forget to increase the version number in the below code, which will be present in the constructor of the class which extends the SQLiteOpenHelper class.
super(Context context, String name, CursorFactory factory, int newVersion)
it is not ideal way to keep altering table dynamically based on user input. Instead create a flexible table that can store key value pair to solve this.
key can be the user entered variable(which you are trying to make a new column name) and value is the content you have to store.

Will my app update sucessfully in market if i add new columns to sqlite?

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

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.

add new table onUpgrade 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

Categories

Resources