SQLite database, onUpgrade for version1 to release - android

OK, so my app is ready for full release. I want to prepare my SQLite db correctly for release. My initial development program utilized the following code for onUpgrade (SQLite db helper class).
#Override
public void onUpgrade(SQLiteDatabase db, int oldVersion, int newVersion) {
db.execSQL("DROP TABLE IF EXISTS " + TABLE_NAME);
onCreate(db);
}
Then, as I needed to update my database, I followed this tutorial to increase the revision numbers, add columns, and all worked very well. For example, my onUpgrade changed to this.
private static final String DATABASE_ALTER_ADD_VELOCITY = "ALTER TABLE walk_run_table ADD COLUMN VELOCITY_CALC REAL";
#Override
public void onUpgrade(SQLiteDatabase db, int oldVersion, int newVersion) {
if (oldVersion < 2) db.execSQL(DATABASE_ALTER_ADD_VELOCITY);
if (oldVersion < 3) db.execSQL(DATABASE_ALTER_ADD_ELEVATION);
}
Now, I'm preparing for release and want to bring my version back down to 1 (since all users will be getting a fresh install of course). Should my onUpgrade revert to "DROP TABLE IF EXISTS".... ? And then proceed with revisions as shown in the tutorial mentioned earlier? This worked well for me in development and I suspect will work well for release, but want to make sure for uploading my program to google play. Is there a better practice to prepare the SQLite db for future revisions?

It does not matter which actual version number you are using; it can go up to four billion.
And future app versions will have to increase the database version anyway; there's nothing gained by setting it to one now.
Your released app will never encounter an old database, so you can remove the code to update from older development versions.
If you do encounter a smaller version number, you have accidentally run it on a development machine, or the database file got corrupted, or someone copied a fake database into your application's storage. In all cases, the correct response is to error out.

Related

Android: Controlling of DB Versions

As I am working on a project that involves multiple developers, and we are using DVCS, each developer will be working on a feature branch assigned to them. That being said, the feature branches are branched out from the latest develop branch. In the develop branch, the db version in the DatabaseHandler is
private static final int DATABASE_VERSION = 2;
In my feature branch, I am required to make a new table. So I have to increase the version to 3, so I can include the following in onUpgrade to execute the CREATE_APPLIANCE_TABLE statement.
#Override
public void onUpgrade(SQLiteDatabase db, int oldVersion, int newVersion) {
Log.d("Database onUpgrade", "Old is: " + oldVersion + ". New is: " + newVersion);
switch(oldVersion) {
case 1:
db.execSQL(CREATE_USER);
case 2:
db.execSQL(CREATE_APPLIANCE_ACTIVITY); // for version 3
}
}
My concern is, what if another developer needs to make a new table too, and he carries out the same process as me. Do he increase the version in his feature branch to 3? And if so, what should be our next step when either or both of us close our feature branch and merged it into develop? Since we both edited the same file, there may be chance of conflict. So in the develop branch, do the version stay at 3, or?
Thanks in advance. :)
As I understand your questions. When you will merge your code files, you can put all your and his tables creating in one database version.
Like that:
public void onUpgrade(SQLiteDatabase db, int oldVersion, int newVersion) {
Log.d("Database onUpgrade", "Old is: " + oldVersion + ". New is: " + newVersion);
switch(oldVersion) {
case **N**:
db.execSQL(CREATE_YOUR_DATABASE);
db.execSQL(CREATE_HIS_DATABASE);
}
As I understand, Whenever two developers work on the same file , at the time of merging branches last one to pull the changes will get the conflict in that file. So the last developer to pull changes can merge the changes manually after resolving all the conflicts.
In your case if version on develop is 2(Assuming version in market apk is 2) then next release version (market apk) for all branches will be 3, provided all features are included in that release.

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

Upgrade SQLite database in Android

I am making one android app where I am using SQLite database. I have already released one version of it and now for second and third version suppose I require to change in the database table like adding/removing fields.
So How can I handle this upgrade in Android. Here I don't want to drop the complete table like stuff, Here it should use Alter to update the tables.
Suppose user has installed my first version of my application then after few days I released next version 1.1 with changes in database table - added one field and here user did not upgrade it and meanwhile I again released the 1.2 again added one more field
So here How I could handle this situation when this user upgrade my application from Version 1 to Version 1.2, where Version 1.1 is missed and attribute which I added is also missed that can create problems.
Any solution to handle this ??
Assuming you're using SQLiteOpenHelper, just increment the database version number for any to-be-released version with database schema changes. In case there's an older database file around, your onUpgrade() will be called so you can migrate the database from any old version.
For example, if your 1.0 database version is 1, 1.1 is 2 and 1.2 is 3 and the user is updating version 1.0 to 1.2. onUpgrade() is called with oldVersion set to 1 and newVersion set to 3. From these version numbers your code can figure out what needs to be done, like:
#Override public void onUpgrade(SQLiteDatabase db, int oldVersion, int newVersion) {
switch (oldVersion) {
case 1:
updateFrom1To2(db);
case 2: // fall-through
updateFrom2To3(db);
break;
default:
Assert.fail("You forgot to write code for oldVersion " + oldVersion);
}
}
You can do anything you want in onUpgrade in sqldb
. You can use ALTER to add new columns to your table.
Worst case, if your schema is completely and entirely different, you'll have to create the new table, populate it using data from the old table, and then delete the old table.
In any case, onUpgrade was designed to allow for a smooth upgrade without any loss of data. It's just up to you to implement it properly.
You should put all changes in your onUpgrade method you can use this code:
#Override
public void onUpgrade(SQLiteDatabase db, int oldVersion, int newVersion) {
String sql = "ALTER TABLE " + TABLE_SECRET + " ADD COLUMN " +
"name_of_column_to_be_added" + " INTEGER";
db.execSQL(sql);
}
for more look this
EDIT
This is a nice thought and i found one solution(not tested), you need
to check the update ,example - Check for the ALTER you have done in 1.1 in the
version 1.2 by reading the db rows or some thing , if it
is not there, you want to Alter this too with the 1.2 alter .
I usually follow a different kind of implementation to onUpgrade method to support backward compatibility in databases.
Note: Here I only show implementation to add more columns in the latest database versions.
public void onUpgrade(SQLiteDatabase db, int oldVersion, int newVersion) {
// 1. Copy the database file to temproary file.
// 2. Drop all the old tables from db
// db.execSQL("DROP TABLE IF EXISTS " + <TABLE NAME> );
// 3. Create tables using new Schema
// this.onCreate(db);
// 4. Copy the data from temproary file to new db
// 5. Remove the temprorary file
}
It is more convenient for me to use this kind of implementation than using ALTER commands.

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.

Categories

Resources