Android SQLiteOpenHelper: Why onCreate() method is not called?

前端 未结 10 832
死守一世寂寞
死守一世寂寞 2020-12-08 18:38

I am trying to make my first Android app. I noticed that the SQLiteOpenHelper.onCreate() method is not called to create tables if the database not exists. Howev

10条回答
  •  [愿得一人]
    2020-12-08 19:04

    I had a similar problem however it was not the OnCreate call that was the issue.

    In the example code above, Kevin explained that the OnCreate is not called if the database already exists. However if, like me, you are using multiple tables from separate activities, then though you may have created the database already, the table associated with this activity may yet have not been created. Hence when you attempt to set the cursor data on a non-existent table, you will invoke an exception.

    My solution was define a separate class called CreateTable which is called both from the OnCreate override and from the constructor after the

    db = getWritableDatabase();

提交回复
热议问题