Sqlite database update in android?

I have an Android application that uses Sqlite as a database.It has the following tables:

  • Hotels
  • Location
  • Favorite

I save my original database file in the assests folder, and when the user installs my application, I just copy this database to the / data / data / package _name / databases directory. Initially, the Favorites table is empty, and it is filled after the user starts resembling hotels. My problem is that I want to run an updated version of the application with some bug fixes and some new hotels added to the database, so I need to update the existing users database with new hotels and locations without affecting the favorites table. Now, if I keep my old approach and update the database version number, then the application will delete the old database and use the new database, but all the data in the favorites table will be lost. I do not want this to happen. Now the problem is how to update Hotels and Locations without losing data in the Favorites table.

+4
source share
3 answers
  • Before updating, write the contents of the previous table to a file and save it on the SD card.

  • Then you can update your database with the new version.

  • And after executing this copy, return the data from the backup file (from the SD card) to the updated database. After successfully copying the backup, delete the file from the SD card.

+1
source

I know this question was asked a long time ago, but I had a similar problem, and I wanted to share my solution, it seems to have helped. I am a newbie, so feel free to contribute -

  @Override public void onUpgrade(SQLiteDatabase db, int oldVersion, int newVersion) { //code to keep table data List<obj> objList = new ArrayList<obj>(); String selectQuery = "SELECT score,list_name,quiz_length FROM obj_table"; Cursor cursor = db.rawQuery(selectQuery, null); // looping through all rows and adding to list if (cursor.moveToFirst()) { do { obj o = new obj(); o.final_score = cursor.getInt(0); o.quiz_name = cursor.getString(1); o.quiz_length = cursor.getInt(2); objList.add(o); } while (cursor.moveToNext()); } //done storing data, now upgrade DB from asset file try { //my db file is upgraded here copyDataBase(); } catch (IOException e) { } //now insert our saved table data for (Score obj_rec: objList){ ContentValues values = new ContentValues(); values.put("score", obj_rec.final_score); values.put("list_name", obj_rec.quiz_name); values.put("quiz_length", obj_rec.quiz_length); db.insert("obj_table", null, values); } } 
+2
source

Typically, a database upgrade should be performed using the SQLiteOpenHelper class. I would advise you to do some tests on your own device before publishing it. You need to increase your version of the database and call the "ALTER TABLE" method from sqlite. This has been discussed in many threads, I think the clearest:

Difficulty updating SQlite table

and here is even an article with some solution:

http://joshhendo.blogspot.de/2012/03/android-sqlite-database-upgrade.html

However, a safe way would be to keep the old database in tempfolder so that the user can return the old one if something is running into chaos.

0
source

Source: https://habr.com/ru/post/1487812/


All Articles