Msgstr "There is no default constructor available in android.database.sqlite.SQLitepenhelper" in Android Studio

An attempt to extend the class using SQLiteOpenHelper, but this error appears: "There is no default constructor available in android.database.sqlite.SQLitepenhelper" together with the other "cannot resolve the symbol Category, Note, ..."

class DbHelper extends SQLiteOpenHelper { @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); } 
+7
android constructor sqlite default sqliteopenhelper
source share
2 answers

You need to define an explicit constructor yourself that calls the 4- or 5-arg super SQLiteOpenHelper in SQLiteOpenHelper .

For example:

 public DbHelper(Context context) { super(context, "database.db", null, 1); } 

where database.db is the name of your database and 1 is the version.

+13
source share

If your DBHelper child, then this post will help, othervise, you can understand everything. First define it, like this one, outside of your 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 the parent class

 public MyDatabase(Context c){ ourContext=c; } 
+2
source share

All Articles