Android Kotlin with Sqlite Connectivity CRUD

Before starting anything,this is my first blog so don’t expect perfection in writing content but the code is well tested so enjoy it !!!.

Tutorial have some steps :
Step 1: Create a Pojo
It is simple to create a pojo in Kotlin like we use to create for Java but there is slight change in syntax ,I am using Users class

class Users (var userID:Int,var userName:String,var userAge:Int){
}

above class takes three argument in its constructor userID,userName and userAge.

Step 2: Create a Helper class to define database related logic

class KotlinHelper(context: Context) : SQLiteOpenHelper(context, DATABASENAME, null, DATABASEVERSION) {
override fun onCreate(p0: SQLiteDatabase?) {
val query = "CREATE TABLE USER (userID TEXT,userName TEXT,userAge INTEGER)"
p0!!.execSQL(query)
Log.v("@@@WWE", " Table Created Sucessfully")
}

override fun onUpgrade(p0: SQLiteDatabase?, p1: Int, p2: Int) {
p0!!.execSQL("DROP TABLE IF EXIST USER")
onCreate(p0)
}

companion object {
private val DATABASENAME = "test"
private val DATABASEVERSION
= 1
}

fun populatePerson(users: Users) {
val db = this.writableDatabase
var values = ContentValues()
values.put("userID", users.userID)
values.put("userName", users.userName)
values.put("userAge", users.userAge)
db.insert("USER", null, values)
db.close()
Log.v("@@@WWe ", " Record Inserted Sucessfully")
}

fun getUsers(): List<Users> {
val db = this.writableDatabase
val list = ArrayList<Users>()
val cusrsor: Cursor
cusrsor = db.rawQuery("SELECT * FROM USER", null)
if (cusrsor != null) {
if (cusrsor.count > 0) {
cusrsor.moveToFirst()
do {
val userID = cusrsor.getInt(cusrsor.getColumnIndex("userID"))
val userName = cusrsor.getString(cusrsor.getColumnIndex("userName"))
val userAge = cusrsor.getInt(cusrsor.getColumnIndex("userAge"))
val user = Users(userID, userName, userAge)
list.add(user)
} while (cusrsor.moveToNext())
}
}
return list
}

fun updateUser(users: Users) {
val db = this.writableDatabase
var values = ContentValues()
values.put("userID", users.userID)
values.put("userName", users.userName)
values.put("userAge", users.userAge)

val retVal = db.update("USER", values, "userID = " + users.userID, null)
if (retVal >= 1) {
Log.v("@@@WWe", " Record updated")
} else {
Log.v("@@@WWe", " Not updated")
}
db.close()

}

fun deleteUser(users: Users) {
val db = this.writableDatabase
var values = ContentValues()
values.put("userID", users.userID)
values.put("userName", users.userName)
values.put("userAge", users.userAge)
val retVal = db.delete("USER", "userID = " + users.userID, null)
if (retVal >= 1) {
Log.v("@@@WWe", " Record deleted")
} else {
Log.v("@@@WWe", " Not deleted")
}
db.close()

}
}

in above code there are four user define method namely populatePerson,getUsers,updateUser and deleteUser to insert ,get,update and delete the user data respectively .

Step 3: Create a android Kotlin activity to implement the helper logic .

val helper = KotlinHelper(this)

helper is instance of our KotlinHelper class

class KotLinDatabaseExample : Activity() {
val helper = KotlinHelper(this)
override fun onCreate(savedInstanceState: Bundle?) {
super.onCreate(savedInstanceState)
setContentView(R.layout.db_dash)
val btnPop = findViewById(R.id.btnPop) as Button
val btnGet = findViewById(R.id.btnGet) as Button
val btnUpdate = findViewById(R.id.btnUpdate) as Button
val btnDelete = findViewById(R.id.btnDelete) as Button


val sachin = Users(1, "Sachin Tendulkar", 40)
val kohli = Users(2, "Kohli", 27)
val dhoni = Users(3, "Dhoni", 36)
val sangakara = Users(4, "Sangakara", 36)

btnPop.setOnClickListener(View.OnClickListener {
helper
.populatePerson(sachin)
helper.populatePerson(kohli)
helper.populatePerson(dhoni)
helper.populatePerson(sangakara)
})
btnGet.setOnClickListener(View.OnClickListener {
printUser(helper.getUsers())
})
btnUpdate.setOnClickListener(View.OnClickListener {
updateUser();
})
btnDelete.setOnClickListener(View.OnClickListener {
deleteUser()
})
}

fun printUser(users: List<Users>) {
for (user: Users in users) {
Log.v("@@@WWE", " Users ID : " + user.userID + " Name : " + user.userName + " Age : " + user.userAge)
}
}

fun updateUser() {
val dhoni = Users(3, "M S Dhoni", 35)
helper.updateUser(dhoni)
}

fun deleteUser() {
val sangakara = Users(4, "Sangakara", 36)
helper.deleteUser(sangakara)
}
}

Thanks for reading this spinet feedback is always welcome