SQLiteOpenHelper failing to call onCreate?

前端 未结 3 1395
南笙
南笙 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:36

    oncreate() method is not a constructor and also Called when the database is created for the first time.So you have to call getWritableDatabase() or getReadableDatabase() for open or create to your database.

    getReadableDatabase() :- Create and/or open a database.

    getWritableDatabase() :- Create and/or open a database that will be used for reading and writing.

    0 讨论(0)
  • 2020-11-30 03:38

    You have to call getWritableDatabase() or getReadableDatabase().

    0 讨论(0)
  • 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

    0 讨论(0)
提交回复
热议问题