Hello, I create a database, the data of which I fill in from cloud storage.

The Database class reads and writes data to the sqlite. I check my internet connection and upgrade.

//options.getVersion - version of the database in the cloud

mDatabaseHelper = new DatabaseHelper(context,"mydatabase.db",null, options.getVersion()); mSqLiteDatabase = mDatabaseHelper.getWritableDatabase(); ContentValues values = new ContentValues(); values.put(mDatabaseHelper.OPTIONS_COLUMN_MAP, options.getMap()); values.put(mDatabaseHelper.OPTIONS_COLUMN_VERSION, options.getVersion()); mSqLiteDatabase.insert(mDatabaseHelper.TABLE_OPTIONS, null, values); 

Next, I need to make a request in some fragment. For example:

  mDatabaseHelper = new DatabaseHelper(context,"mydatabase.db",null, ??????); // здесь проблема mSqLiteDatabase = mDatabaseHelper.getWritableDatabase(); TextView textView = (TextView) getView().findViewById(R.id.textView4); Cursor c = mSqLiteDatabase.rawQuery("SELECT * FROM options", null); if (c.moveToFirst()) { textView.setText(c.getString(c.getColumnIndex(mDatabaseHelper.COLUMN_ABOUT))); } 

The problem is that I don’t know what version to indicate to me in the constructor. options.version will be zero when the cloud is unavailable.

Error: Can't downgrade database from version 25 to 1

Or maybe I’ll even incorrectly organize the reading of the base in the fragment

    1 answer 1

    When reading from a fragment, according to your code, you must refer to your local database. The version of the database in this case is determined by you. Those. it has no relation to the DB version. Thus, when opening your local DB with a helper, use the constructor:

     mDatabaseHelper = new DatabaseHelper(this); 

    If the data in the database changes, then the version should not be changed. The database version should be changed when its structure is changed, and since your database depends on the cloud database, you will change your database accordingly when the database changes in the cloud. In doing so, you will need to override the onUpgrade method in your helper class.

    • Thank you very much! for some reason I thought that to update the database, you need to specify a new version) - andrei piletsky
    • @andreipiletsky is not for that) no, when updating data, the database version does not change. If we have solved your question, mark it as resolved and useful - ZigZag