-1

This is some part of my database helper class

public class DatabaseHandler extends SQLiteOpenHelper {
    private static final int DB_VERSION=5;
    private static final String DB_NAME="Database";
    private static final String Task_Table="TaskTable";
    public static final String List_Table="ListTable";
    private static final String Details_Task="TaskDetails";

    private static final String id="_id";
    private static final String listname="listname";
    private static final String taskname="taskname";
    private static final String completed="completed";
    private static final String favourite="favourite";
    private static final String alarmtime="alarmtime";
    private static final String note="note";
    private static final String totaltask="totaltask";
    private static final String listimage="listimage";


    String query;
    public DatabaseHandler(Context context){
        super(context,DB_NAME,null,DB_VERSION);
        Log.e("super","called");
    }

    @Override
    public void onCreate(SQLiteDatabase db) {
        Log.d("database created","dddd");
        query="create table "+Task_Table+"("+id+" integer primary key,"
                +listname+" text,"+taskname+" text,"+completed+
                " boolean,"+favourite+")";
        db.execSQL(query);
        query="create table "+List_Table+"("+id+" integer primary key,"
                +listname+" text,"+totaltask+" integer"
                //+listimage+" blob"
                +")";
        db.execSQL(query);
        query="create table "+Details_Task+"("+id+" integer primary key,"
                +listname+" text,"+taskname+" text,"+alarmtime+
                " text,"+note+" text"+")";
        db.execSQL(query);


    }

    @Override
    public void onUpgrade(SQLiteDatabase db, int oldVersion, int newVersion) {
        Log.e("upgrade","called");
            db.execSQL("drop table if exists "+Task_Table);
        db.execSQL("drop table if exists "+List_Table);
        db.execSQL("drop table if exists "+Details_Task);
        Log.e("calling","oncreate");
        onCreate(db);
        Log.e("back","again");
    }
}

When I change database version then only OnUpgrade() is called ,call of Oncreate() inside it doesn't work. I have tried using getReadableDatabase() and getWritabledatabase() but it also doesn't work I have tried uninstalling app,clearing data but nothing is working. Please help

Phantômaxx
  • 37,901
  • 21
  • 84
  • 115
shubhamjuneja
  • 395
  • 1
  • 5
  • 16
  • 2
    http://stackoverflow.com/questions/21881992/when-is-sqliteopenhelper-oncreate-onupgrade-run – laalto Dec 02 '16 at 12:30

3 Answers3

1

In sqlite onCreate call once while first time while you create instance of database helper class. and onUpgrade method call when new version is grater then old version (newVersion>oldVersion);

Surya Prakash Kushawah
  • 3,185
  • 1
  • 22
  • 42
1

Calling onCreate() wont work if the database already created. Check this answer for further details: stackoverflow link

Community
  • 1
  • 1
Shahbaz Ahmed
  • 1,190
  • 10
  • 13
0

When you launch android app for the first time in emulator or android mobile your DB will be created and your onCreate() method will be called.

When you change DB_VERSION so that its greater then older version then your onUpgrade() method will be called.

If you are testing your android app and you want to call onCreate() method whenever you install it, you have to uninstall it from emulator or android mobile. Once you uninstall then installation of android app will call onCreate() method.

But keep in mind if you uninstalled the app then it does not matter if your DB_VERSION is greater then old version, it will call onCreate() only.

SachinSarawgi
  • 2,632
  • 20
  • 28