“There is no default constructor available in android.database.sqlite.SQLitepenhelper” in Android Studio

前端 未结 2 824
长发绾君心
长发绾君心 2021-01-17 16:19

Trying to extend class with SQLiteOpenHelper, but this error shows up : \"There is no default constructor available in android.database.sqlite.SQLitepenhelper\" along with o

2条回答
  •  一向
    一向 (楼主)
    2021-01-17 17:00

    If your DBHelper child then this post help, othervise you can allready understandfirst Define it like this one in outside of you class, means uperside...

    private DBHelper ourHelper;
    private final Context ourContext;
    

    Then use this

    class DbHelper extends SQLiteOpenHelper {
    public DBHelper(Context context) {
            super(context, DB_NAME, null, DB_VIRSION);
            // TODO Auto-generated constructor stub
        }
    
    @Override
    public void onCreate(SQLiteDatabase db) {
    
        db.execSQL(Category.getSql());
        db.execSQL(Note.getSql());
        db.execSQL(Attachment.getSql());
        db.execSQL(CheckItem.getSql());
    }
    
    @Override
    public void onUpgrade(SQLiteDatabase db, int oldVersion, int newVersion) {
        db.execSQL("DROP TABLE IF EXISTS " + Category.TABLE_NAME);
       db.execSQL("DROP TABLE IF EXISTS " + Note.TABLE_NAME);
        db.execSQL("DROP TABLE IF EXISTS " + Attachment.TABLE_NAME);
        db.execSQL("DROP TABLE IF EXISTS " + CheckItem.TABLE_NAME);
    
        onCreate(db);
    }
    

    And then try this context for your parent class

    public MyDatabase(Context c){
        ourContext=c;
    }
    

提交回复
热议问题