2

I've got an Android app I'm about ready to release, and I'm reviewing some of my code. I'm worried about my implementation for my SQLiteOpenHelper. Specifically, I want to verify what the oldVersion and newVersion reference in the onUpgrade method. Is this based upon the versionCode in my AndroidManifest.xml? Or is this value something completely separate, specific to the database? If it's the latter, how does the database determine the version?

Matt Huggins
  • 81,398
  • 36
  • 149
  • 218
  • Latter, you just need to declare a simple int variable in your SQLiteOpenHelper class, that is the only variable you need to change every time you update anything. – Chor Wai Chun Jun 23 '13 at 15:16

2 Answers2

4

Is this based upon the versionCode in my AndroidManifest.xml?

Not normally, unless you upgrade your schema every time you publish an upgrade to your app, and even then the "based upon" would be mostly coincidental.

Or is this value something completely separate, specific to the database?

Yes. You increment the version every time you have a new database schema.

If it's the latter, how does the database determine the version?

On every onCreate() and onUpgrade(), Android sticks the then-new version of the database schema in a metadata table in the database. That's what Android then checks when you use your helper in the future, comparing that stored value to the one you provide in your SQLiteOpenHelper constructor.

CommonsWare
  • 986,068
  • 189
  • 2,389
  • 2,491
  • So where does the "then-new version" value come from? Is it always 1 whenever `onCreate` is called? Or is there a way for me to specify what this value should be? The reason I ask is that I'm running 5 migrations from `onCreate`. How can I tell Android to start at migration #6 the next time it needs to update? – Matt Huggins Jun 23 '13 at 16:35
  • Ahh, I think I realized what I'm missing. The constructor takes a version number, which I totally forgot I'm overriding. That's where the version number comes from. :) – Matt Huggins Jun 23 '13 at 17:00
  • @MattHuggins: IOW, the version number comes from you. Or your loyal minion who is typing in the code, whichever the case may be. :-) – CommonsWare Jun 23 '13 at 19:21
0

while you solved your question, that you can determine it just yourself on creation. I want to leave this for persons wondering:

how does the database determine the version..

.. if the database already exists?

it is stored with the db file and accessible via PRAGMA user_version; as noted in SQLiteOpenHelper.java:getVersion().

wbob
  • 431
  • 3
  • 9