The bottom line is that I constantly make changes to the database, but when I rebuild a project, the application works with the old version, I have to delete the application from the device and install it on a new one. Is there a way to update the imported database?
- I do this: when I install the application, I create / copy a database. At this point, in Preferences, I save the application code. If I made changes to it myself later, then just like you, I need to update it. I change the application code and when opening the main activity I check the current code and the saved one, if they do not match, I copy / update the database. Well, again I save the new code in the settings. - dubok79
- I do not change the code. I only add info in the database to add the old version so that the old version is deleted, and the new one is installed. - Lev Polyakov
- for this, the application code is used. Increase its number - it will be a flag that you need to update the database or show the update window or something. - dubok79
- If you are too lazy to write code that monitors the relevance of the database, you can simply delete the database from the device (and not the entire application), a new one will be created. How to find the database on the device - pavlofff
- How to delete a database? - Lev Polyakov
3 answers
I do this: when I install the application, I create / copy a database. At this point, in Preferences, I save the application code. If I made changes to it myself later, then just like you, I need to update it. I change the application code and when opening the main activity I check the current code and the saved one, if they do not match, I copy / update the database. Well, again I save the new code in the settings.
public static void checkVersionCode(Context context) { boolean flagNewVersion = false; int cur_vc = 0; try {cur_vc = context.getPackageManager().getPackageInfo(context.getPackageName(), 0).versionCode; }catch(NameNotFoundException e) {} SharedPreferences mVersion = null; try {mVersion = context.getSharedPreferences(VERSION_FILENAME,Context.MODE_PRIVATE); } catch (Exception e) { } finally { try { if (mVersion.contains(VERSION_CODE)) { int pref_vc = mVersion.getInt(VERSION_CODE, 0); if (pref_vc != cur_vc) flagNewVersion = true; } else flagNewVersion = true; } catch (Exception e) {} } if (flagNewVersion == true) { //здесь копируется или обновляется БД setVersionCode(context, cur_vc);//сохраняем новую версию } }
If the database structure does not change, you can simply fill in the new database in the old one, ignoring the same lines. For this new database we put in assets.
In one project I did this:
public DBHelper(Context context) { super(context, DATABASE_NAME, null, DATABASE_VERSION); this.myContext = context; create_db(); dbQueryManager = new DBQueryManager(getReadableDatabase()); dbUpdateManager = new DBUpdateManager(getWritableDatabase()); boolean dbexist = checkdatabase(); if (dbexist) { merge_db(DATABASE_VERSION_OLD, DATABASE_VERSION); } } private boolean checkdatabase() { boolean checkdb = false; try { String myPath = DB_PATH + DATABASE_NAME_OLD; File dbfile = new File(myPath); checkdb = dbfile.exists(); } catch (SQLiteException e) { System.out.println("Database doesn't exist"); } return checkdb; } public void create_db() { InputStream myInput = null; OutputStream myOutput = null; try { File file = new File(DB_PATH + DATABASE_NAME); if (!file.exists()) { this.getReadableDatabase(); //получаем локальную бд как поток myInput = myContext.getAssets().open(DATABASE_NAME); // Путь к новой бд String outFileName = DB_PATH + DATABASE_NAME; // Открываем пустую бд myOutput = new FileOutputStream(outFileName); // побайтово копируем данные byte[] buffer = new byte[1024]; int length; while ((length = myInput.read(buffer)) > 0) { myOutput.write(buffer, 0, length); } myOutput.flush(); myOutput.close(); myInput.close(); } } catch (IOException ex) { } } public void merge_db(int oldVersion, int newVersion) { if (newVersion != oldVersion) { open(); database.execSQL("ATTACH DATABASE '" + DB_PATH + File.separator + DATABASE_NAME_OLD + "' AS Old_DB"); database.execSQL("INSERT OR IGNORE INTO product (title) SELECT task_title FROM Old_DB.product;"); } } public void open() throws SQLException { String path = DB_PATH + DATABASE_NAME; database = SQLiteDatabase.openDatabase(path, null, SQLiteDatabase.OPEN_READWRITE); }
Maybe not quite right, but I'm just a novice developer :)
look at the onUpgrade (...) SQLOpenHelper method
This method is called if you increment the database version here you can add new tables, modify old ones and migrate data from the old version to the new one.
The sequence is as follows:
- Take the data
- Modify the database (the easiest option is to delete everything -> create a new structure)
- We roll the data back to the new database