With the GUI, everything is fine, but when I try to do it without the GUI, I get errors.
Is this possible? I'm new to Kotlin. Thank you very much.
You probably need to post some code so people can help you.
package com.example.sql_lite_dos
data class Rubro (val id:String, val detalle:String,val inc:Float)
package com.example.sql_lite_dos
import android.content.ContentValues
import android.content.Context
import android.database.sqlite.SQLiteDatabase
import android.database.sqlite.SQLiteOpenHelper
//class dbOpenHelper (context:Context): SQLiteOpenHelper
// (context,DATABASE_NAME,null,DATABASE_VERSION){
class dbOpenHelper (context:Context): SQLiteOpenHelper
(context,DATABASE_NAME,null,DATABASE_VERSION){
override fun onCreate(p0: SQLiteDatabase?) {
TODO("Not yet implemented")
}
override fun onUpgrade(
p0: SQLiteDatabase?,
p1: Int,
p2: Int
) {
TODO("Not yet implemented")
}
companion object {
private const val DATABASE_NAME="database.db"
private const val DATABASE_VERSION=3
private const val TABLE_NAME="rubros"
private const val COLUMN_ID="id"
private const val COLUMN_DETALLE="detalle"
private const val COLUMN_INC="inc"
}
fun insertRubros(rubro:Rubro) {
val db=this.writableDatabase
val values= ContentValues().apply {
put(COLUMN_ID,rubro.id)
put(COLUMN_DETALLE,rubro.detalle)
put(COLUMN_INC, rubro.inc)
}
db.insert(TABLE_NAME,null,values)
db.close()
}
}
/// i can't insert record here (error declare lateinit)
package com.example.sql_lite_dos
import android.database.sqlite.SQLiteDatabase
import android.database.sqlite.SQLiteOpenHelper
import com.example.sql_lite_dos.dbOpenHelper
fun main(){
lateinit var db:dbOpenHelper
val registro=Rubro("ASX","ASCII TABLE",3.25f)
//db= dbOpenHelper(this)
db.insertRubros(registro)
}
Sorry i forgot, 3 files
You can take a look at Exposed if you are already familiar with SQL things.
I mean looking at your code, it looks pretty obvious? You’ve declared a variable called db, but never assigned a value to it. You then try to use db, but it doesn’t have a value. Change db from lateinit var to val, and the compiler will tell you the problem.
1 Like