@laalto is right, you need to re-create your table once you’ve dropped it, you need to add a call to onCreate(p0)
inside your onUpgrade()
method.
I removed a couple of unneeded fields on your class, this is how it looks now:
class DbManger(context: Context) {
val dbName = "MyNotes"
val dbTable = "Notes"
val colID = "ID"
val colTitle = "Title"
val colDesc = "Desc"
var dbVer = 4
val sqlCreateTable = "CREATE TABLE IF NOT EXISTS $dbTable($colID INTEGER PRIMARY KEY,$colTitle TEXT,$colDesc TEXT);"
var sqlDataBase: SQLiteDatabase? = null
inner class dbHelperNotes(val context: Context) : SQLiteOpenHelper(context, dbName, null, dbVer) {
override fun onCreate(database: SQLiteDatabase?) {
database?.execSQL(sqlCreateTable)
Toast.makeText(context, "Database is created", Toast.LENGTH_LONG).show()
}
override fun onUpgrade(database: SQLiteDatabase?, oldVersion: Int, newVersion: Int) {
database?.execSQL("drop table IF EXISTS " + dbTable)
onCreate(database)
}
}
fun insert(values: ContentValues): Long {
val id = sqlDataBase!!.insert(dbTable, "", values)
return id
}
init {
val db = dbHelperNotes(context)
sqlDataBase = db.writableDatabase
}
}
I believe you also have a typo on your class name, should be ‘DbManager’.
0
solved Error in my first table in kotlin