I read on this SO thread(answer from @RobinM) that the SQLiteOpenHelper constructor increases the databases version number everytime its called. Now, i checked the class documentation on the Android developer site and couldn't find any evidence of this. My question is two-fold. Is this behaviour true, and if it is, is there a way of preventing unexpected version number increments that will trigger onUpgrade(), and potentially wipe existing data?
Asked
Active
Viewed 1,287 times
0
-
1"I read on this SO thread(answer from @RobinM) that the SQLiteOpenHelper constructor increases the databases version number everytime its called" -- your claim appears nowhere in [that answer](http://stackoverflow.com/a/30542949/115145). – CommonsWare May 26 '16 at 19:04
-
scroll down...its not in the first answer, look for RobinM's answer – lll May 26 '16 at 19:09
-
I linked to [RobinM's answer](http://stackoverflow.com/a/30542949/115145). I read [RobinM's answer](http://stackoverflow.com/a/30542949/115145). Your claim appears nowhere in [RobinM's answer](http://stackoverflow.com/a/30542949/115145). – CommonsWare May 26 '16 at 19:14
-
Read my comment on the ticked answer – lll May 26 '16 at 19:16
-
you misunderstood the answer. the version is set by you, and is applied when you call the constructor. to avoid wiping data, avoid upgrading your database, or having a migration script – njzk2 May 26 '16 at 19:33
1 Answers
1
If you have a look at
SQLiteOpenHelper(Context context, String name,
SQLiteDatabase.CursorFactory factory, int version)
You supply the database version.
The constructor gets called when you create your helper and if you supply a version
higher than the current version, the version of the db will be updated and onUpgrade
will be called.
The version will not increase without your doing. To increase the db version you need to call the constructor with a higher version.

David Medenjak
- 33,993
- 14
- 106
- 134
-
I see.. @RobinM's answer implied that this may happen under certain conditions. His exact words are: NOTE: calling the DatabaseHelper constructor updates the db version After the constructor call, the db was tagged with the new version. Kill the app before a call to getWritableDatabase() or getReadableDatabase() and you're on the new version. Thereafter new executions never call the onUpgrade method until DATABASE_VERSION is increased again. (sigh! now it seems ridiculously obvious :) My suggestion is to add some sort of "checkDatabaseVersion()" to the early stages of your app...." – lll May 26 '16 at 19:14
-
-
@VernonKawonza Robin seems confused. He debugged and got side effects due to stopping app executions at various points. Just read the end of his answer, and it says the same. – David Medenjak May 26 '16 at 19:16