Search code examples
androiddatabasesqlitesqliteopenhelper

How to get an version of database schema?


I want to upgrade database schema to a new version. Before I do so, firstly I will need to acquire some data from the existing database. The problem is with if I attempt to read database with these:

DatabaseHelper dbHelper = new DatabaseHelper(context, DB_VERSION);
SQLiteDatabase database = dbHelper.getReadableDatabase();

it is possible it will be upgraded (in DatabaseHelper.onUpgrade()), which is an unwanted behavior as I just want to extract some records from the old, not upgraded, version. I would think I can do it by passing current version in DB_VERSION, but how I can get the current version of the database (remember, I'm upgrading the whole app to next version)?

Or is there another way of getting some records from database without causing an upgrade?


Solution

  • Use SQLiteDatabase directly and execute the query to get the version. It won't try to upgrade or otherwise modify it. Open read-only for extra protection :)