SQLITE - Installing a Version of a New Database

I used this example to create a database that I copied on a new installation

http://www.reigndesign.com/blog/using-your-own-sqlite-database-in-android-applications/

Problem: now I have done some database updates, and I figured that I also do this update for the main file in the assembly so that users always get the latest version of the new installation

I have another class that handles all db request request requests.

I set this line

private static final int DATABASE_VERSION = 5; 

On a new installation, the database is now copied correctly, but when the query class calls the databasehelper method again, the onupgrade () method is called and tries to upgrade to the latest version, and the application crashes when it tries to perform updates that cannot be performed

I was in the understanding that the following sets the version of the database on fresh installations or it is wrong. If so, how can I install the database version for new installations

 public DatabaseHelper(Context context) { super(context, DB_NAME, null, DATABASE_VERSION); this.context = context; } 

just for completeness, there is an onupgrade () sample here

 @Override public void onUpgrade(SQLiteDatabase db, int oldVersion, int newVersion) { dbUpgrade = new DatabaseUpgrades(context); Log.d(DEBUG_TAG, "Calling onupgrade db"); Log.d(DEBUG_TAG + " : " + DatabaseHelper.class.getName(), "Upgrading database from version " + oldVersion + " to " + newVersion); if (oldVersion == 1) { Log.d(DEBUG_TAG, "Updating to Version 2"); dbUpgrade.upgradeDB2(db); oldVersion++; } } 

Question: which version is the new database installed for the new installation, and how can it be rewritten if it is not version 5

thanks

+6
source share
3 answers

It is right:

 public DatabaseHelper(Context context) { super(context, DB_NAME, null, DATABASE_VERSION); /* ... */ } 

I put the source code below getWritableDatabase() , and as you can see, it will not call onUpgrade() if the version in the db file does not match the current new version that you pass in the constructor (line version != mNewVersion ). Therefore, either your database file is not overwritten or the version number in your new database is incorrect. Please check this out.

 /** * Create and/or open a database that will be used for reading and writing. * Once opened successfully, the database is cached, so you can call this * method every time you need to write to the database. Make sure to call * {@link #close} when you no longer need it. * * <p>Errors such as bad permissions or a full disk may cause this operation * to fail, but future attempts may succeed if the problem is fixed.</p> * * @throws SQLiteException if the database cannot be opened for writing * @return a read/write database object valid until {@link #close} is called */ public synchronized SQLiteDatabase getWritableDatabase() { if (mDatabase != null && mDatabase.isOpen() && !mDatabase.isReadOnly()) { return mDatabase; // The database is already open for business } if (mIsInitializing) { throw new IllegalStateException("getWritableDatabase called recursively"); } // If we have a read-only database open, someone could be using it // (though they shouldn't), which would cause a lock to be held on // the file, and our attempts to open the database read-write would // fail waiting for the file lock. To prevent that, we acquire the // lock on the read-only database, which shuts out other users. boolean success = false; SQLiteDatabase db = null; if (mDatabase != null) mDatabase.lock(); try { mIsInitializing = true; if (mName == null) { db = SQLiteDatabase.create(null); } else { db = mContext.openOrCreateDatabase(mName, 0, mFactory); } int version = db.getVersion(); if (version != mNewVersion) { db.beginTransaction(); try { if (version == 0) { onCreate(db); } else { onUpgrade(db, version, mNewVersion); } db.setVersion(mNewVersion); db.setTransactionSuccessful(); } finally { db.endTransaction(); } } onOpen(db); success = true; return db; } finally { mIsInitializing = false; if (success) { if (mDatabase != null) { try { mDatabase.close(); } catch (Exception e) { } mDatabase.unlock(); } mDatabase = db; } else { if (mDatabase != null) mDatabase.unlock(); if (db != null) db.close(); } } } 

EDIT
You can check the version in a new database file with the following query:

 PRAGMA user_version; 

It should return 5 in your case.

+4
source

He got a method called onUpgrade (SQLiteDatabase db, int oldVersion, int newVersion) {do your upgrade.,}

if you use the SQLiteOpenHelper class, then you can override this method.

0
source

just create

 @Override public void onCreate(SQLiteDatabase db) { onUpgrade(db, 0, DATABASE_VERSION); 
0
source

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


All Articles