Home » Android » android – Version number for SQLite DB

android – Version number for SQLite DB

Posted by: admin May 14, 2020 Leave a comment

Questions:

I am working on sqlite in my android app.

I am doing db.getVersion() to get current version of my database.

it is showing 3 in logcat.

even in constructor i set 4 as a version number.

public Helper(Context context)
    {
        super(context, DATABASE_NAME, null,4);
    }

i am giving 4 it should take 4 as version number.

but its not behaving like this. it is showing 3 as version number.

please any suggestion.

UPDATE:-

    public class Helper extends SQLiteOpenHelper {
        public static final String  DATABASE_NAME = "helper.db";

        public static final String TITLE = "title";
        public static final String AUTHOR = "author";
        public static final String ISBN = "isbn";

        public Helper(Context context)
        {
            super(context, DATABASE_NAME, null,4);
        }

        @Override
        public void onCreate(SQLiteDatabase db) {

                db.execSQL( "CREATE TABLE book1(_id INTEGER PRIMARY KEY AUTOINCREMENT,title TEXT, author TEXT,isbn INTEGER);");
                Log.v("Create Table", "CREATE TABLE book1(_id INTEGER PRIMARY KEY AUTOINCREMENT,title TEXT, author TEXT,isbn INTEGER);");

Log.v("version ", "Version number is "+db.getVersion());

}
How to&Answers:

You are checking the version number in the onCreate function, why? the OnCreate function should only be called the first time the datbase is created from scratch, in this case it should build the DB as you require fitting to the version you are using.

Can you try checking the DB version after the DB has been created and you ask to get a readable or writable instance of it?

SQLiteDatabase db = new Helper(context).getWritableDatabase();
db.getVersion(); // what value do you get here?

check out the source code of android 4.1.1 SQLiteOpenHelper, here it seems as the db version should be 0 inside the onCreate function, I guess it might be implemented in another way on the version you are testing on.
http://grepcode.com/file/repository.grepcode.com/java/ext/com.google.android/android/4.1.1_r1/android/database/sqlite/SQLiteOpenHelper.java#242

Answer:

It seems like the db is already created by previous version of your code, try uninstalling the app from the device you’re testing on , then re-install. this should give you version = 4

Answer:

You have to drop the table inside oldversion database, override this function

 @Override
    public void onUpgrade(SQLiteDatabase db, int oldVersion, int newVersion) {
        db.execSQL("DROP TABLE IF EXISTS book1");
        onCreate(db);
    }

that should be it.

to avoid data loss refer to this tutorial

http://denverdroid.blogspot.com/2010/04/how-to-non-destructibly-upgrade-your.html

or this

How to update an SQLite Database and NOT lose all existing data?

Answer:

I think you’re providing the wrong constructor for your Helper class. Try this:

  public Helper(Context context, String name, CursorFactory factory, int version) {
     super(context, name, factory, version);
  }

Your current constructor is telling Android that it is already at version 4 and there is no need to upgrade it.

Also, you need to provide an onUpgrade method as well.

  @Override
  public void onUpgrade(SQLiteDatabase database, int oldVersion, int newVersion) {
    // upgrade code
  }

Answer:

TO CLARIFY THIS ISSUE ONCE AND FOR ALL:

 private SQLiteDatabase getDatabaseLocked(boolean writable) {
  ...
            db.beginTransaction();
            try {
                if (version == 0) {
   -------->      onCreate(db);      <---- YOUR QUERY ON VERSION IS HERE !!!
                } else {
                    if (version > mNewVersion) {
                        onDowngrade(db, version, mNewVersion);
                    } else {
                        onUpgrade(db, version, mNewVersion);
                    }
                }
   -------->    db.setVersion(mNewVersion);      <----   NEW VERSION IS SET HERE !!!
                db.setTransactionSuccessful();
            } finally {
                db.endTransaction();
            }
        }
...
}

SOURCE:
public final class SQLiteDatabase