Someone
Someone

Reputation: 19

Can somebody explain to me the Sqlite version thing

i have this code

@Database(entities = { Report.class }, version = 1)

from what i have read or was able to collect , is that you need to change the version every time the user updates or deletes any of his own data he entered,but then i read that if you add a new column to the database as a developer you need to update the version and then check which version the user has and make a switch case to update user database from the old version to the new one and in some they save the old version and the new version but why i am a little bit confused here.

so my question is when,how and where do we use this version code.

Upvotes: 0

Views: 82

Answers (1)

Dave Thomas
Dave Thomas

Reputation: 3837

You change the version code when the schema of your database changes. You don't change the version number when the user changes their data. The schema is the structure of your database. Ie. the table definitions.

Let's say you release a version of your app to the store and your user database table looks something like this ( annotations left out for brevity ):

@Entity
class User {
   val username: String
   val likesStarWars: Boolean
}

You then go back to development and create a new feature for your app that let's you store the user's favourite colour as well, and maybe it also needs to store whether they use Canadian/British spellings of words.

Now the table looks like this:

@Entity
class User {
   val username: String
   val likesStarWars: Boolean
   val britishEnglish: Boolean
   val favouriteColour: String
}

At this point before you release a new version to the store you must increase your database version from 1 to 2. This signals to any user devices that had previously installed your application that the database must be upgraded.

In order to support those older versions of the database you must provide code to migrate from version 1 of your database to version 2. https://developer.android.com/training/data-storage/room/migrating-db-versions.html

You don't have to worry about checking which version of the database a user has, if you implement your Room database properly with migrations you will be covered by the Room database library. The library will handle checking and updating the database for you.

In the end this version code is a very important number that you should take the time to understand the intricacies of. During the first release of your app before you ever reach the store your database will only ever be version 1. After that, when you are releasing your first update, and there are database structure changes, then you must consider the version number.

Upvotes: 1

Related Questions