user3454856
user3454856

Reputation: 17

What is the use of DB_VERSION attribute in Sq Lite database in android?

I am using Sq Lite database in my android application. I created helper class to create tables in Sq Lite. In helper class i used DB_VERSION attribute and I used DB_VERSION to call super class(SQLiteOpenHelper) constructor. Can anyone tell me what is the use of DB_VERSION attribute in db helper class.

Thank you.

Upvotes: 0

Views: 760

Answers (1)

juanmeanwhile
juanmeanwhile

Reputation: 2634

db_version is a value you specify manually. When a database is created, it stores this db_version you set manually for further uses.

Everytime the helper class iniziates it checks if db_version is the same than the one internally stored. If this number is different, then onUpgrade() method is call yo you can perform upgrade operations. Let me put you an example:

  1. You create an app and with db_version == 1.
  2. You develop a new functionality wich introduces changes in the database (for example, a new column). You will change db_version to 2. In the onUpgrade() method, you will add the new column to all existing rows.
  3. When you intall the new version of your app in the device, when you first open your db, the version it is checked. Your database has version 1 but db_version is 2 now, so onUpgrade() is called and the new column added.

And that is the use.

Upvotes: 1

Related Questions