Android Kotlin SQLite Demo

Here is the example of Android with Kotlin SQLite database in Android Studio.

MainActivity.kt

package com.example.sqlitedemo

import android.content.Context

// Author : www.raviroza.com
// Date : 26-Dec-2022, 10.00 am

import android.database.Cursor
import android.database.sqlite.SQLiteDatabase
import androidx.appcompat.app.AppCompatActivity
import android.os.Bundle
import android.widget.Toast
import kotlinx.android.synthetic.main.activity_main.*

class MainActivity : AppCompatActivity() {

    lateinit var DB : SQLiteDatabase
    lateinit var QRY : String
    lateinit var CURSOR : Cursor

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

        DB = openOrCreateDatabase("MyDB", Context.MODE_PRIVATE,null)

        Toast.makeText(this,DB.path.toString(),Toast.LENGTH_LONG).show()

        QRY = "CREATE TABLE IF NOT EXISTS student(rollno VARCHAR,name VARCHAR,marks VARCHAR);"

        DB.execSQL(QRY)

        Toast.makeText(this,"Table Created !",Toast.LENGTH_LONG).show()
        loadData()

        btnAdd.setOnClickListener()
        {
            QRY = "insert into student values ( ${txtRollNo.text} , '${txtName.text}' , ${txtMarks.text} )"
            DB.execSQL(QRY)
            Toast.makeText(this,"Record Inserted !",Toast.LENGTH_LONG).show()
            txtName.text = null
            txtMarks.text = null
            txtRollNo.text = null
            loadData()

        }

    }
    fun loadData()
    {
        QRY = "Select * from Student "
        CURSOR = DB.rawQuery(QRY,null)
        var data : String = ""
        while (CURSOR.moveToNext())
        {
            data += CURSOR.getString(0) + "\n"
            data += CURSOR.getString(1) + "\n"
            data += "-------------------------------\n"
        }
        if(data.length==0) {

            lblResult.text = "No Records Found"
        }
        else
        {
            lblResult.text = data
        }
    }
}

activity_main.xml

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

<!--    Author : www.raviroza.com-->
<!--    Date : 26-Dec-2022, 10.00 am-->

    <TextView
        android:layout_width="match_parent"
        android:layout_height="wrap_content"
        android:text="DB App Demo - Student"
        android:textSize="25dp"
        android:gravity="center"
        android:background="@color/colorAccent"
        android:padding="8dp"
         />

    <TextView
        android:layout_width="match_parent"
        android:layout_height="wrap_content"
        android:text="Enter Roll No. :"
        android:textSize="20dp"
        />

    <EditText
        android:id="@+id/txtRollNo"
        android:layout_width="match_parent"
        android:layout_height="wrap_content"
        android:text="0"
        android:inputType="number"
        android:textSize="20dp"

        />

    <TextView
        android:layout_width="match_parent"
        android:layout_height="wrap_content"
        android:text="Enter Name :"
        android:textSize="20dp"
        />

    <EditText
        android:id="@+id/txtName"
        android:layout_width="match_parent"
        android:layout_height="wrap_content"
        android:textSize="20dp"
        />

    <TextView
        android:layout_width="match_parent"
        android:layout_height="wrap_content"
        android:text="Enter Marks:"
        android:textSize="20dp"
        />

    <EditText
        android:id="@+id/txtMarks"
        android:layout_width="match_parent"
        android:layout_height="wrap_content"
        android:textSize="20dp"
        android:inputType="number"
        />

    <Button
        android:id="@+id/btnAdd"
        android:layout_width="match_parent"
        android:layout_height="wrap_content"
        android:textSize="25dp"
        android:text="Add Record"
        android:textAllCaps="false"
        />
    <ScrollView
        android:layout_width="match_parent"
        android:layout_height="wrap_content">

    <TextView
        android:id="@+id/lblResult"
        android:layout_width="match_parent"
        android:layout_height="wrap_content"
        android:layout_margin="4dp"
        android:text="student data"
        android:textSize="18dp"
        />

    </ScrollView>

</LinearLayout>

Leave a Comment