How create various tables in Kotlin
I am upgrading from Java to Kotlin, and I need to create several tables in a DB, first I create a companion object and define the table, in the DBHelper class, I create the database and try to call the query, but it does not create any table. This always works in Java, need understand how works companion. I’m already new to Kotlin.
class SpeciesHelper {
companion object{
const val TABLE_NAME: String = "species"
const val COLUMN_ID: String = "id"
const val COLUMN_NAME: String = "name"
const val COLUMN_DESC: String = "description"
const val COLUMN_IMAGE: String = "image"
const val COLUMN_SKILL: String = "skill"
const val COLUMN_TYPE: String = "type"
const val COLUMN_STAR: String = "star"
val createTable = buildString {
append("CREATE TABLE $TABLE_NAME (")
append("$COLUMN_ID} INTEGER PRIMARY_KEY,")
append("$COLUMN_NAME} TEXT,")
append("$COLUMN_DESC} TEXT,")
append("$COLUMN_IMAGE} TEXT,")
append("$COLUMN_SKILL} TEXT,")
append("$COLUMN_TYPE} INTEGER,")
append("$COLUMN_STAR} TEXT)")
}
}
}
class DBHelper(context: Context?) : SQLiteOpenHelper(context, DATABASE_NAME, null, DATABASE_VERSION) {
companion object {
const val DATABASE_NAME: String = "db_species"
const val DATABASE_VERSION: Int = 1
}
override fun onCreate(db: SQLiteDatabase?) {
db?.execSQL(SpeciesHelper.createTable)
db?.execSQL(StarsHelper.createTable)
}
override fun onUpgrade(p0: SQLiteDatabase?, p1: Int, p2: Int) {
TODO("Not yet implemented")
}
}