DATABASE SQLITE
activity.xml
====================================================


<?xml version="1.0" encoding="utf-8"?>
<LinearLayout
    xmlns:android="http://schemas.android.com/apk/res/android"
    android:layout_width="match_parent"
    android:layout_height="match_parent"
    android:orientation="vertical"
    android:padding="16dp">

    <!-- First Name Input -->
    <EditText
        android:id="@+id/fname"
        android:layout_width="match_parent"
        android:layout_height="wrap_content"
        android:hint="Enter First Name"
        android:inputType="text"/>

    <!-- Last Name Input -->
    <EditText
        android:id="@+id/lname"
        android:layout_width="match_parent"
        android:layout_height="wrap_content"
        android:hint="Enter Last Name"
        android:inputType="text"/>

    <!-- Buttons -->
    <Button
        android:id="@+id/btnadd"
        android:layout_width="match_parent"
        android:layout_height="wrap_content"
        android:text="Add Student"
        android:onClick="btn_click"/>

    <Button
        android:id="@+id/btnupd"
        android:layout_width="match_parent"
        android:layout_height="wrap_content"
        android:text="Update Student"
        android:onClick="btn_click"/>

    <Button
        android:id="@+id/btndel"
        android:layout_width="match_parent"
        android:layout_height="wrap_content"
        android:text="Delete Student"
        android:onClick="btn_click"/>

    <Button
        android:id="@+id/btnlis"
        android:layout_width="match_parent"
        android:layout_height="wrap_content"
        android:text="List Students"
        android:onClick="btn_click"/>

    <!-- Student List Display -->
    <TextView
        android:id="@+id/textView"
        android:layout_width="match_parent"
        android:layout_height="wrap_content"
        android:textSize="16sp"
        android:padding="8dp"/>

</LinearLayout>

==============================================================
kt
===============================================================
package com.example.sqlitedatabase

import android.os.Bundle
import android.support.v7.app.AppCompatActivity
import android.support.v7.app.AlertDialog
import android.view.View
import android.widget.EditText
import android.widget.TextView

class MainActivity : AppCompatActivity() {
    private var fname: EditText? = null
    private var lname: EditText? = null
    private var list: TextView? = null
    private var dbc: dbController? = null

    override fun onCreate(savedInstanceState: Bundle?) {
        super.onCreate(savedInstanceState)
        setContentView(R.layout.activity_main)

        fname = findViewById(R.id.fname)
        lname = findViewById(R.id.lname)
        list = findViewById(R.id.textView)
        dbc = dbController(this, "", null, 1)
    }

    fun btn_click(v: View) {
        when (v.id) {
            R.id.btnadd -> dbc?.insert(fname?.text.toString(), lname?.text.toString())
            R.id.btndel -> dbc?.delete(fname?.text.toString())
            R.id.btnupd -> {
                val dialog = AlertDialog.Builder(this@MainActivity)
                dialog.setTitle("Enter Firstname")
                val newFname = EditText(this)
                dialog.setView(newFname)
                dialog.setPositiveButton("OK") { _, _ ->
                    dbc?.update(fname?.text.toString(), newFname.text.toString())
                }
                dialog.show()
            }
            R.id.btnlis -> dbc?.list(list)
        }
    }
}
===============================================================
dbController.kt
==============================================================
package com.example.sqlitedatabase

import android.content.ContentValues
import android.content.Context
import android.database.sqlite.SQLiteDatabase
import android.database.sqlite.SQLiteOpenHelper
import android.widget.TextView

class dbController(context: Context, name: String, factory: SQLiteDatabase.CursorFactory?, version: Int) :
    SQLiteOpenHelper(context, "std.db", factory, version) {

    override fun onCreate(db: SQLiteDatabase) {
        db.execSQL("CREATE TABLE STUDENT(FNAME TEXT, LNAME TEXT)")
    }

    override fun onUpgrade(db: SQLiteDatabase, oldVersion: Int, newVersion: Int) {
        db.execSQL("DROP TABLE IF EXISTS STUDENT")
        onCreate(db)
    }

    fun insert(a: String, b: String) {
        val cv = ContentValues().apply {
            put("FNAME", a)
            put("LNAME", b)
        }
        writableDatabase.use { db ->
            db.insert("STUDENT", null, cv)
        }
    }

    fun delete(a: String) {
        writableDatabase.use { db ->
            db.delete("STUDENT", "FNAME=?", arrayOf(a))
        }
    }

    fun update(a: String, aa: String) {
        writableDatabase.use { db ->
            val cv = ContentValues().apply {
                put("FNAME", aa)
            }
            db.update("STUDENT", cv, "FNAME=?", arrayOf(a))
        }
    }

    fun list(tv: TextView?) {
        tv?.text = ""

        readableDatabase.use { db ->
            val c = db.rawQuery("SELECT * FROM STUDENT", null)
            c.use {
                while (c.moveToNext()) {
                    tv?.append("${c.getString(0)} ${c.getString(1)}\n")
                }
            }
        }
    }
}