SQLiteOpenHelper failing to call onCreate?

前端 未结 3 1405
南笙
南笙 2020-11-30 03:04

I am trying to create a local database on an android phone using sqlite.

I have a helper class, shown below, that is used to create the database and pro

3条回答
  •  借酒劲吻你
    2020-11-30 03:40

    The onCreate is not a constructor for the database class. It is only called if you try to open a database that does not exist. To open/create the database you need to add a call to one of these methods:

    public class SmartApp extends Activity implements OnSharedPreferenceChangeListener {
       private SmartDBHelper dBHelper;
       public void onCreate(Bundle savedInstanceState) {
          //where i am wanting to create the database and tables
          dBHelper = new SmartDBHelper(getContext());
          // open to read and write
          dBHelper.getWritableDatabase();
          // or to read only
          // dBHelper.getReadableDatabase();
       }
    }
    

    It is a bit big but here is my DatabaseAdapter you can take a look at: http://saintfeintcity.org/projects/sfc/repository/entry/trunk/src/org/saintfeintcity/database/GameDbAdapter.java

提交回复
热议问题