SQLite database add new table when upgrade app - android

I add one more table to my app's database.
However, if I upgrade my app without clearing its data, the new table doesn't seem to be added automatically.
Therefore, some code that need to retrieve data from the new table fail.
Is it possible to add new tables automatically on app upgrade?

Please implement onUpgrade (SQLiteDatabase db, int oldVersion, int newVersion) method.

There is an Android helper class to manage database creation and version management using an application's raw asset files.
Have a look at this :
https://github.com/jgilfelt/android-sqlite-asset-helper

Yes there is a method onUpgrade
add you code there and increase database version if your version is higher than the previous
onUpgrade is fired and the your code will execute which you write to add table
sample
Remember increasing you version code in super call
class Database extends SQLiteOpenHelper
{
public Database(Context _myContext)
{
super(_myContext, DB_NAME, null, DB_VERSION_CODE);
}
#Override
public void onUpgrade(SQLiteDatabase db, int oldVersion, int newVersion)
{
if(oldVersion > newVersion)
{
// create a new table what you want to create you can also check the existence of the table
}
}
}

Related

SQlite "no such table" after updating application

I updated my application today and started seeing the following in crashlitics:
android.database.sqlite.SQLiteException: no such table: LESSONATTACHMENT (code 1 SQLITE_ERROR[1]): , while compiling: SELECT _THUMB_PATH FROM LESSONATTACHMENT WHERE _id = 2
I create a new table inside my SQLiteOpenHelper, it was not in the application before I update.
This is how I created the table:
public void onCreate(SQLiteDatabase db) {
db.execSQL(CREATE_TABLE_LESSON_ATTACHMENTS);
}
Should I instead place it in OnUpgrade? Like this:
public void onUpgrade(SQLiteDatabase db, int oldVersion, int newVersion) {
db.execSQL(CREATE_TABLE_LESSON_ATTACHMENTS);
}
Whenever you want to make changes in tables or create a table on app update, you have to change the version of database. In your case let us suppose you have upgraded from dbversion from 2 to 3 and you want to create a table for versions greater than 2.Then you have to create a table in onUpgrade like below:
#Override
public void onUpgrade(SQLiteDatabase db, int oldVersion, int newVersion) {
if(newVersion>=3){
db.execSQL("CREATE TABLE IF NOT EXISTS `TABLE_NAME` ....");
}
}
here I am creating the table for all the versions greater than 2 since there is a possibility that dbversion gets upgraded from 2 to 4 or some other version greater than 3.
You Need to increase your Database Version and create the new Table in onUpgrade and Maybe delete the old Table.
Your Database is created only once. Upgrades are done in the onUpgrade function
You can also delete the Database and it will be created new with the new Table, but it depends if you are able to do this at the Moment.
for example this Code will upgrade your Database up to the current Version
#Override
public void onUpgrade(SQLiteDatabase db, int oldVersion, int newVersion) {
switch(oldVersion){
case 1:
//Changes between version 1 and 2
case 2:
// Changes between Version 2 and 3
case currentversion:
//changes between last change and now
}
}
when you update your database just put a new case with the new Version below this. This way you can maintain every Version of Database and make sure that it will be upgraded properly. Important is, that you don't use a break after each case.
this Code Shows for example one of my Databases, when I increase the Version I simply add a new case for the old Version. When you havent upgraded your Database yet, set the new Version to 1 higher than your old Version and add a case clause for the old Version.
public class database extends SQLiteOpenHelper {
private static String DATABASE = "Database";
private static int VERSION = 4;
public database(#Nullable Context context) {
super(context, DATABASE, null, VERSION);
}
#Override
public void onCreate(SQLiteDatabase db) {
db.execSQL("CREATE DATABASE TABLE");
}
#Override
public void onUpgrade(SQLiteDatabase db, int oldVersion, int newVersion) {
switch(oldVersion){
case 1:
db.execSQL("CHANGES DONE AFTER VERSION 1");
case 2:
db.execSQL("CHANGES DONE AFTER VERSION 2");
case 3:
db.execSQL("CHANGES DONE AFTER VERSION 3");
}
}

onUpgrade in sqlite external database in android

I am developing a database application and I am stuck up at one place.
How to upgrade external database after adding new table in Database in android sqlite?
after updating my app I got error: No such table found in database
In SQLiteOpenHelper class
public abstract void onUpgrade (SQLiteDatabase db,
int oldVersion,
int newVersion)
Called when the database needs to be upgraded. The implementation should use this method to drop tables, add tables, or do anything else it needs to upgrade to the new schema version.
You have to Update the Version Number
public class DBHelper extends SQLiteOpenHelper {
public DBHelper(){
super(context,DATABASE_NAME,null,VersionNumber);// Change Version here
}
public void onCreate(SQLiteDatabase db) {}
//While upgrading pass new version
public void onUpgrade(SQLiteDatabase database, int oldVersion, int newVersion) {}
}
Example
How to use onUpgrade() correctly

How can I add new columns to a SQLite database after the Android app is released?

I want to add new columns to a SQLite database, but I already released my app on the Play Store so, if I edit it, users need to uninstall and reinstall the app,
but I don't want that. Please, help, I am new to Android.
(1) Increment (or simply change) your database version
(2) It would lead to onUpgrade() method call
(3) Execute your query(for adding a new column) in the onUpgrade() method.
The Right Way of doing is mentioned here in this blog.
Sometimes user may update the version 1.5 from the version 1.1.In other words, They may skip the other versions between the 1.1 to 1.5. You might changed database couple of time between 1.1 to 1.5. So in order to give the user a benefit of all the database changes, you need to take of onUpgrade() method as below.
#Override
public void onUpgrade(SQLiteDatabase db, int oldVersion, int newVersion) {
if (oldVersion < 2) {
db.execSQL(DATABASE_ALTER_TEAM_1);
}
if (oldVersion < 3) {
db.execSQL(DATABASE_ALTER_TEAM_2);
}
}
Hope it helps.
You need to changes in following method of Database Class
#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);
}

delete sqlite database when updating new version of application

I have uploaded an apk (version 1.0) with 22 tables in SQLite Database on Google Playstore.
Now i want to update database with 36 tables in new version (version 2.0) of application.
I am storing datebase at default location so when i press "Clear
data" in Application Manager, database is gonna deleted.
I just want to know how to delete old database (same as clear data) when user update new version?
Update:
If is there any solution for clear data while updating application from play store then also answered me.
Your help would be appreciated.
Thanks.
you can use this method to delete your database.
context.deleteDatabase(DATABASE_NAME);
You can also use this method to find your database path first and then delete that.
File myDb = context.getDatabasePath(DATABSE_NAME);
Boolean isDelete = myDb.delete();
The other solution is , if you want to update your database then just change your version number of database. onUpgrade() will automatically get called and your old database will be deleted and new database will be created.
Finally done with simple solution:
/** UPGRADE DATABASE **/
#Override
public void onUpgrade(SQLiteDatabase db, int oldVersion, int newVersion) {
Log.i(TAG, "Database Version: OLD: "+ oldVersion + " = NEW: "+newVersion);
if(context.deleteDatabase(DATABASE_NAME))
Log.i(TAG, "Database Deleted....");
else
Log.i(TAG, "Database Not Deleted..");
}
Nice Question. Just follow the steps.
1) There are two methods that you override on Your Helper class
#Override
public void onCreate(SQLiteDatabase db) {
}
#Override
public void onUpgrade(SQLiteDatabase db, int oldVersion, int newVersion) {
if(oldVersion!=newVersion){
// Remove all old tables and crate new ones
}
}
2) create an object of Helper class.
MyOpenHelper dbHelper = new MyOpenHelper(this,"dbname", 1);
3) Now just increase the database version when you want to change the database.(Third argument in MyOpenHelper class. "1" is a database version).
4) When database version changes it will give you a callback in onUograde() method.
5) Remove all tables in onUpgrade() method and create new ones.
6) That's it.
EXPLANATION :
Your app version 1.1 is on google play and your database version is "1". Now you want to upload version 1.2 with new database. Just set database version "2" in your helper file. And check the the oldVersion and newVersion in onUpgrade() method if both are not same that means application updating and remove old tables and create new ones.
If you are using SQLiteOpenHelper just delete your tables in your onUpgrade method and recreate all your tables again. You should have not problems at all.
Check this out.
Hope it helps
Note: Be careful with database versions, if you are playing with your database numbers make sure you have the right number as your oldVersion or it will not work properly when you update your google play.
The obvious solution is to delete the old DB file when onUpgrade() is called and copy the new version over. Too bad it does not work. You get an error something like this:
Caused by: android.database.sqlite.SQLiteReadOnlyDatabaseException: attempt to write a readonly database (code 1032)
SQLiteLog﹕ (1032) statement aborts at 4: [PRAGMA user_version = 2]
SQLiteLog﹕ (28) file unlinked while open: /data/data/my.package/databases/my_db.db
You don't need to delete the database, just copy over it using the method you've alady defined (copyDataBase), like this:
#Override
public void onUpgrade(SQLiteDatabase db, int oldVersion, int newVersion) {
Toast.makeText(myContext, "onUpgrade called!", Toast.LENGTH_LONG).show();
if (oldVersion < newVersion) {
Log.v("Database Upgrade", "Database version higher, upgrading");
try {
copyDataBase();
} catch (IOException e) {
throw new Error("Error upgrading database");
}
}
}
wrok for me, thank

onUpgrade in sqlite database in android

I am developing a database application and I am stuck up at one place.
The scenario is: I have a database and I am maintaining the version of the database.
It is the same as that of the application version.
Now I am implementing the database in version 1. There are 3 tables getting created in version 1. In version 2, I am upgrading the database by adding one more table. Hence, the query is in the upgrade().
Now, what if the user installs version 2. The onUpgrade() will not get called because there is no database. Hence, the onCreate() will be called and the consequence will be it will create only 3 tables.
I was thinking to call the onUpgrade() explicitly in the onCreate(). But many developers on stackoverflow have suggested not to call it explicitly.
I am completely stranded.
Thanks in advance.
I know it's an old post, you have certainly found a solution, but I find the question valid (upvoted) and as it did not receive an answer, here are my 2 cents:
Calling onUpgrade() from onCreate() should work (I did not test it), but there is a better way to organize you helper (see snippet hereafter). The key point is that both those methods are just entry points.
public class SampleDBHelper extends SQLiteOpenHelper {
private static final String DB_NAME = "mydatabase";
private static final int DB_VERSION = 2;
public SampleDBHelper(Context context) {
super(context, DB_NAME, null, DB_VERSION);
}
#Override
public void onCreate(SQLiteDatabase db) {
updateDatabase(db, 0, DB_VERSION);
}
#Override
public void onUpgrade(SQLiteDatabase db, int oldVersion, int newVersion) {
updateDatabase(db, oldVersion, newVersion);
}
private void updateDatabase(SQLiteDatabase db, int oldVersion, int newVersion) {
if (oldVersion < 1) {
// Create 3 initial tables
}
if (oldVersion < 2) {
// Create the new table
}
}
}
The advantages of this kind of helper:
You never need to change onCreate() and onUpgrade() methods, only updateDatabase().
For each new version, you just need to :
increment DB_VERSION
add a new section if (oldVersion < N) { // do the upgrade }.
Credit : Strongly inspired from Head First Android Development, Dawn and David Griffiths, O'Reilly, 2015

Categories

Resources