Good day. There are two classes: the class contract - ClassContract and the class where it is created, opens the database class - ClassDBHeler. I use a ready-made database, which I put in the assets folder of the project and when the user installs the application, the database is copied to it and he uses the application.
Here is the class ClassContract
public class ClassContract { private ClassContract() { }; public static final class ClassEntry implements BaseColumns { public final static String TABLE_NAME = "table1"; public final static String _ID = BaseColumns._ID; public final static String COLUMN_WORD = "field1"; public final static String COLUMN_TRANSLATION = "field2"; public final static String COLUMN_TYPE = "field3"; public final static String COLUMN_FAVORITES = "field4"; } } Here is the class ClassDBHeler
public class ClassDBHeler extends SQLiteOpenHelper { private static String DB_PATH = "/data/data/ru.mysite.project1/databases/"; private static final String DATABASE_NAME = "dbase.db"; private static final int DATABASE_VERSION = 1; public SQLiteDatabase database; private Context myContext; public ClassDBHeler(Context context) { super(context, DATABASE_NAME, null, DATABASE_VERSION); this.myContext = context; } 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 + DATABASE_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 = myContext.getAssets().open(DATABASE_NAME); String outFileName = DB_PATH + DATABASE_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 + DATABASE_NAME; database = SQLiteDatabase.openDatabase(myPath, null, SQLiteDatabase.OPEN_READONLY); } @Override public synchronized void close() { if(database != null) database.close(); super.close(); } @Override public void onCreate(SQLiteDatabase db) { } @Override public void onUpgrade(SQLiteDatabase db, int oldVersion, int newVersion) { } } I updated the database and I want these updates to occur in users. In the onUpgrade method, how can I access the database that users already have? How do I understand the db parameter in onUpgrade is a new database, and how to access the table from the old database?