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

前端 未结 10 871
死守一世寂寞
死守一世寂寞 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 18:44

    Call getWritableDatabase(); in the constructor

    public DataBaseH(@Nullable Context context) {
        super(context, dataBaseName, null, dataBaseVersion);
        SQLiteDatabase db=this.getWritableDatabase();
    
    }
    
    @Override
    public void onCreate(SQLiteDatabase db) {
     String createTable="CREATE TABLE IF NOT EXISTS "+tableName+                              " ( "+
                id+  " INTEGER NOT NULL PRIMARY KEY AUTOINCREMENT,"+
                name+                              " TEXT,"+
                familyName+                        " TEXT,"+
                age+                           " INTEGER);";
    
        db.execSQL(createTable);
    
         Log.i(TAG,"db.exect");
    
    
    }
    

提交回复
热议问题