Error In My First Table In Kotlin
I'm facing a problem when I try to insert a column in the database using Kotlin in Android. This is the error: 08-04 19:45:03.781 14302-14302/com.example.hello.note E/Zygote: v2
Solution 1:
Your database version is already at 4 but your onUpgrade
merely drops the table but does not create the required tables.
If you want data-losing upgrades, you can just invoke onCreate
in onUpgrade
after dropping old tables. Or at development time, it's often just easier to uninstall and reinstall your app. See When is SQLiteOpenHelper onCreate() / onUpgrade() run? for more about how SQLiteOpenHelper
works.
Solution 2:
@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:
classDbManger(context: Context) {
val dbName = "MyNotes"val dbTable = "Notes"val colID = "ID"val colTitle = "Title"val colDesc = "Desc"var dbVer = 4val sqlCreateTable = "CREATE TABLE IF NOT EXISTS $dbTable($colID INTEGER PRIMARY KEY,$colTitle TEXT,$colDesc TEXT);"var sqlDataBase: SQLiteDatabase? = nullinnerclassdbHelperNotes(val context: Context) : SQLiteOpenHelper(context, dbName, null, dbVer) {
overridefunonCreate(database: SQLiteDatabase?) {
database?.execSQL(sqlCreateTable)
Toast.makeText(context, "Database is created", Toast.LENGTH_LONG).show()
}
overridefunonUpgrade(database: SQLiteDatabase?, oldVersion: Int, newVersion: Int) {
database?.execSQL("drop table IF EXISTS " + dbTable)
onCreate(database)
}
}
funinsert(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'.
Post a Comment for "Error In My First Table In Kotlin"