Read only SQLite DB in the resource folder for the new version of the application

I am currently working with sql databases in android and created my first application. Now I wanted to update this application and the database in the resource folder. I overwritten the old database with the new one, but on my phone it does not show new records.

It somehow saves old records / queries or completely old Db. I checked on the net but could not find a solution. Below is my dbhelper code. Do I have to do something in onUpgrade or what will happen to the procedure?

Thank you for help!

 public class DataBaseHelper extends SQLiteOpenHelper { private static String DB_PATH; private static String DB_NAME; private SQLiteDatabase db_object; private final Context context; public DataBaseHelper(Context context, String db) { super(context, db, null, 80); this.context = context; DB_PATH = "/data/data/" + context.getPackageName() + "/databases/"; // DB_PATH = "/data/data/com.comp.appname/databases/"; DB_NAME = "mydatabase.sqlite"; try { createDataBase(); } catch (IOException ioe) { throw new Error("Unable to create database"); } try { openDataBase(); } catch (SQLException sqle) { throw sqle; } } @Override public void onCreate(SQLiteDatabase db) { // TODO Auto-generated method stub } @Override public void onUpgrade(SQLiteDatabase db, int oldVersion, int newVersion) { // TODO Auto-generated method stub } public void createDataBase() throws IOException { boolean dbExist = checkDataBase(); if (dbExist) { } else { this.getReadableDatabase(); try { copyDataBase(); } catch (IOException e) { throw new Error("Error copying database"); } } } private boolean checkDataBase() { SQLiteDatabase checkDB = null; try { String myPath = DB_PATH + DB_NAME; checkDB = SQLiteDatabase.openDatabase(myPath, null, SQLiteDatabase.OPEN_READONLY); } catch (SQLiteException e) { } if (checkDB != null) { checkDB.close(); } return checkDB != null ? true : false; } private void copyDataBase() throws IOException { InputStream myInput = context.getAssets().open(DB_NAME); String outFileName = DB_PATH + DB_NAME; OutputStream 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(); } public void openDataBase() throws SQLException { String myPath = DB_PATH + DB_NAME; db_object = SQLiteDatabase.openDatabase(myPath, null, SQLiteDatabase.OPEN_READONLY); } @Override public synchronized void close() { if (db_object != null) db_object.close(); super.close(); } } 
+4
source share
2 answers

I solved my problem:

in the onUpgrade method you need to put:

 mycontext.deleteDatabase(DB_NAME); 

in the databasehelper class, I need to increase the version number, for example, as follows:

 super(context, db, null, 84); 

with my 84, but basically you just need to increase it by 1, stating that this is a higher version. The optimum would be if it was your version of the database that also increased with each external editing of your db.

Hope this helps others.

+1
source

Here is the code to update the database:

 @Override public void onUpgrade(SQLiteDatabase db, int oldVersion, int newVersion) { if (newVersion > oldVersion) { switch (oldVersion) { case 1: executeSQLScript(database, "update_v2.sql"); case 2: executeSQLScript(database, "update_v3.sql"); } } } 

and also take a look at this link: http://www.drdobbs.com/database/using-sqlite-on-android/232900584?pgno=2

+2
source

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


All Articles