0% found this document useful (0 votes)
3 views20 pages

Sqlite Example

The document provides a detailed example of an SQLite application with various XML layout files for activities such as inserting, updating, deleting, and viewing data. It includes Kotlin code for a DatabaseHelper class that manages database operations and a MainActivity that navigates between different functionalities. The application is structured to allow users to perform CRUD operations on a simple database of information.

Uploaded by

vibepuja123
Copyright
© All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
3 views20 pages

Sqlite Example

The document provides a detailed example of an SQLite application with various XML layout files for activities such as inserting, updating, deleting, and viewing data. It includes Kotlin code for a DatabaseHelper class that manages database operations and a MainActivity that navigates between different functionalities. The application is structured to allow users to perform CRUD operations on a simple database of information.

Uploaded by

vibepuja123
Copyright
© All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as PDF, TXT or read online on Scribd

SQLITE EXAMPLE

(1)activity_main.xml

<?xml version="1.0" encoding="utf-8"?>


<RelativeLayout
xmlns:android="[Link]
xmlns:app="[Link]
xmlns:tools="[Link]
android:layout_width="match_parent"
android:layout_height="match_parent"
tools:context=".MainActivity">

<TextView
android:textColor="@color/material_dynamic_neutral0"
android:layout_marginTop="20dp"
android:textStyle="bold"
android:id="@+id/txtlbl1"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_centerHorizontal="true"
android:fontFamily="cursive"
android:text="SQLite Demo Program"
android:textSize="25dp" />

<Button
android:layout_marginTop="20dp"
android:layout_below="@+id/txtlbl1"
android:textSize="20dp"
android:layout_centerHorizontal="true"
android:text="Insert Data"
android:id="@+id/btninsert"
android:layout_width="wrap_content"
android:layout_height="wrap_content"/>

<Button
android:layout_marginTop="20dp"
android:layout_below="@+id/btninsert"
android:textSize="20dp"
android:layout_centerHorizontal="true"
android:text="Update Data"
android:id="@+id/btnupdate"
android:layout_width="wrap_content"

BY : RAVI TALSANIA 1
SQLITE EXAMPLE

android:layout_height="wrap_content"/>

<Button
android:layout_marginTop="20dp"
android:layout_below="@+id/btnupdate"
android:textSize="20dp"
android:layout_centerHorizontal="true"
android:text="Delete Data"
android:id="@+id/btndelete"
android:layout_width="wrap_content"
android:layout_height="wrap_content"/>

<Button
android:layout_marginTop="20dp"
android:layout_below="@+id/btndelete"
android:textSize="20dp"
android:layout_centerHorizontal="true"
android:text="View Data"
android:id="@+id/btnview"
android:layout_width="wrap_content"
android:layout_height="wrap_content"/>

</RelativeLayout>

BY : RAVI TALSANIA 2
SQLITE EXAMPLE

(2)activity_insertdata.xml

<?xml version="1.0" encoding="utf-8"?>


<RelativeLayout
xmlns:android="[Link]
xmlns:app="[Link]
xmlns:tools="[Link]
android:layout_width="match_parent"
android:layout_height="match_parent"
tools:context=".MainActivity">

<TextView
android:textColor="@color/material_dynamic_neutral0"
android:layout_marginTop="20dp"
android:textStyle="bold"
android:id="@+id/txtlbl1"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_centerHorizontal="true"
android:fontFamily="cursive"
android:text="SQLite Demo Program"
android:textSize="25dp" />

<Button
android:layout_marginTop="20dp"
android:layout_below="@+id/txtlbl1"
android:textSize="20dp"
android:layout_centerHorizontal="true"
android:text="Insert Data"
android:id="@+id/btninsert"
android:layout_width="wrap_content"
android:layout_height="wrap_content"/>

<Button
android:layout_marginTop="20dp"
android:layout_below="@+id/btninsert"
android:textSize="20dp"
android:layout_centerHorizontal="true"
android:text="Update Data"
android:id="@+id/btnupdate"
BY : RAVI TALSANIA 3
SQLITE EXAMPLE

android:layout_width="wrap_content"
android:layout_height="wrap_content"/>

<Button
android:layout_marginTop="20dp"
android:layout_below="@+id/btnupdate"
android:textSize="20dp"
android:layout_centerHorizontal="true"
android:text="Delete Data"
android:id="@+id/btndelete"
android:layout_width="wrap_content"
android:layout_height="wrap_content"/>

<Button
android:layout_marginTop="20dp"
android:layout_below="@+id/btndelete"
android:textSize="20dp"
android:layout_centerHorizontal="true"
android:text="View Data"
android:id="@+id/btnview"
android:layout_width="wrap_content"
android:layout_height="wrap_content"/>

</RelativeLayout>

BY : RAVI TALSANIA 4
SQLITE EXAMPLE

(3) activity_updatedata.xml

<?xml version="1.0" encoding="utf-8"?>


<RelativeLayout
xmlns:android="[Link]
xmlns:app="[Link]
xmlns:tools="[Link]
android:layout_width="match_parent"
android:layout_height="match_parent"
tools:context=".updatedata">

<TextView
android:textColor="@color/material_dynamic_neutral0"
android:layout_marginTop="20dp"
android:textStyle="bold"
android:id="@+id/txtlbl1"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_centerHorizontal="true"
android:fontFamily="cursive"
android:text="SQLite Update Data"
android:textSize="25dp" />

<EditText
android:inputType="text"
android:layout_below="@+id/txtlbl1"
android:layout_margin="20dp"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:id="@+id/edid"
android:textSize="20dp"
android:hint="Enter ID"
/>

<EditText
android:inputType="text"
android:layout_below="@+id/edid"

BY : RAVI TALSANIA 5
SQLITE EXAMPLE

android:layout_margin="20dp"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:id="@+id/edname"
android:textSize="20dp"
android:hint="Enter Name"
/>

<EditText
android:inputType="number"
android:layout_below="@+id/edname"
android:layout_margin="20dp"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:id="@+id/edmob"
android:textSize="20dp"
android:hint="Enter Mobile no"
/>

<Button
android:id="@+id/btnupdate"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_below="@+id/edmob"
android:layout_centerHorizontal="true"
android:layout_marginTop="20dp"
android:text="Update"
android:textSize="20dp" />

</RelativeLayout>

BY : RAVI TALSANIA 6
SQLITE EXAMPLE

(4) activity_deletedata.xml

<?xml version="1.0" encoding="utf-8"?>


<RelativeLayout
xmlns:android="[Link]
xmlns:app="[Link]
xmlns:tools="[Link]
android:layout_width="match_parent"
android:layout_height="match_parent"
tools:context=".deletedata">

<TextView
android:textColor="@color/material_dynamic_neutral0"
android:layout_marginTop="20dp"
android:textStyle="bold"
android:id="@+id/txtlbl1"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_centerHorizontal="true"
android:fontFamily="cursive"
android:text="SQLite Delete Data"
android:textSize="25dp" />

<EditText
android:id="@+id/edid"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:layout_below="@+id/txtlbl1"
android:layout_marginStart="20dp"
android:layout_marginTop="20dp"
android:layout_marginEnd="20dp"
android:layout_marginBottom="20dp"
android:hint="Enter ID"
android:inputType="number"
android:textSize="20dp" />

<Button
android:layout_marginTop="20dp"
android:layout_below="@+id/edid"

BY : RAVI TALSANIA 7
SQLITE EXAMPLE

android:textSize="20dp"
android:layout_centerHorizontal="true"
android:text="Delete"
android:id="@+id/btndelete"
android:layout_width="wrap_content"
android:layout_height="wrap_content"/>

</RelativeLayout>

BY : RAVI TALSANIA 8
SQLITE EXAMPLE

(5) activity_viewdata.xml

<?xml version="1.0" encoding="utf-8"?>


<RelativeLayout
xmlns:android="[Link]
xmlns:app="[Link]
xmlns:tools="[Link]
android:layout_width="match_parent"
android:layout_height="match_parent"
tools:context=".viewdata">
<TextView
android:textColor="@color/material_dynamic_neutral0"
android:layout_marginTop="20dp"
android:textStyle="bold"
android:id="@+id/txtlbl1"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_centerHorizontal="true"
android:fontFamily="cursive"
android:text="SQLite View Data"
android:textSize="25dp" />

<TextView
android:id="@+id/txtdata"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:layout_below="@+id/txtlbl1"

android:layout_marginStart="20dp"
android:layout_marginTop="20dp"
android:layout_marginEnd="20dp"
android:layout_marginBottom="20dp" />

</RelativeLayout>

BY : RAVI TALSANIA 9
SQLITE EXAMPLE

BY : RAVI TALSANIA 10
SQLITE EXAMPLE

CODE FILES
(1)[Link]

package [Link]

import [Link]
import [Link]
import [Link]
import [Link]
import [Link]

class DatabaseHelper(context:Context) :
SQLiteOpenHelper(context,DATABASE_NAME,null,1)
{
companion object
{
val DATABASE_NAME="[Link]"
val TABLE_NAME="information"
val COL_1="ID"
val COL_2="NAME"
val COL_3="MOB"
}

override fun onCreate(p0: SQLiteDatabase?)


{
if (p0 != null)
{
[Link]("CREATE TABLE $TABLE_NAME (ID INTEGER PRIMARY KEY
AUTOINCREMENT,NAME TEXT,MOB TEXT)")
}
}

override fun onUpgrade(p0: SQLiteDatabase?, p1: Int, p2: Int)


{
if (p0 != null) {
[Link]("DROP TABLE IF EXISTS"+ TABLE_NAME)
}
onCreate(p0)
}

fun insertData(name:String,mobile:String):Boolean
{
val db=[Link]
val contentValues=ContentValues();
[Link](COL_2,name)
[Link](COL_3,mobile)
var result:Long=[Link](TABLE_NAME,null,contentValues)
[Link]()
return true
}
fun updateData(id:String,name:String,mobile:String):Boolean
{

BY : RAVI TALSANIA 11
SQLITE EXAMPLE

val db=[Link]
val contentValues=ContentValues();
[Link](COL_1,id)
[Link](COL_2,name)
[Link](COL_3,mobile)
[Link](TABLE_NAME,contentValues,"ID=?", arrayOf(id))
return true
}

fun deleteData(id:String):Int
{
val db=[Link]
return [Link](TABLE_NAME,"ID=?", arrayOf(id))
}

val allData : Cursor


get()
{
val db=[Link]
val res=[Link]("select * from "+ TABLE_NAME,null)
return res
}

BY : RAVI TALSANIA 12
SQLITE EXAMPLE

(2) [Link]
package [Link]

import [Link]
import [Link]
import [Link]
import [Link]

class MainActivity : AppCompatActivity() {

lateinit var btninsert:Button


lateinit var btnupdate:Button
lateinit var btndelete:Button
lateinit var btnview:Button

override fun onCreate(savedInstanceState: Bundle?) {


[Link](savedInstanceState)
setContentView([Link].activity_main)

btninsert=findViewById([Link])
btnupdate=findViewById([Link])
btndelete=findViewById([Link])
btnview=findViewById([Link])

[Link] {

val intent= Intent(this,insertdata::[Link])


startActivity(intent)
}

[Link] {
val intent1=Intent(this,updatedata::[Link])
startActivity(intent1)
}

[Link] {
val intent2=Intent(this,deletedata::[Link])
startActivity(intent2)
}

[Link] {
val intent3=Intent(this,viewdata::[Link])
startActivity(intent3)
}
}

BY : RAVI TALSANIA 13
SQLITE EXAMPLE

(3)[Link]
package [Link]

import [Link]
import [Link]
import [Link]
import [Link]
import [Link]

class insertdata : AppCompatActivity() {


lateinit var edname:EditText
lateinit var edmob:EditText
lateinit var btninsert:Button

internal var dbHelper=DatabaseHelper(this)

override fun onCreate(savedInstanceState: Bundle?) {


[Link](savedInstanceState)
setContentView([Link].activity_insertdata)

val actionbar = supportActionBar


actionbar!!.title = "SQLite Insert Data"
[Link](true)

edname=findViewById([Link])
edmob=findViewById([Link])
btninsert=findViewById([Link])

[Link] {

val info_name=[Link]()
val info_mob=[Link]()

if(info_name=="" || info_mob=="")
{
[Link](this,"Provide
Value",Toast.LENGTH_LONG).show()
}
else
{
val result
:Boolean=[Link](info_name,info_mob)
[Link](this,""+result,Toast.LENGTH_LONG).show()

if(result==true)
{
[Link](this,"Record
Saved",Toast.LENGTH_LONG).show()
}
else
{
[Link](this,"Record Not
Saved",Toast.LENGTH_LONG).show()
}

BY : RAVI TALSANIA 14
SQLITE EXAMPLE

}
}
}
override fun onSupportNavigateUp(): Boolean
{
onBackPressed()
return true
}
}

BY : RAVI TALSANIA 15
SQLITE EXAMPLE

(4) [Link]

package [Link]

import [Link]
import [Link]
import [Link]
import [Link]
import [Link]

class updatedata : AppCompatActivity()


{
internal var dbHelper=DatabaseHelper(this)
lateinit var edid:EditText
lateinit var edname:EditText
lateinit var edmob:EditText
lateinit var btnupdate:Button

override fun onCreate(savedInstanceState: Bundle?) {


[Link](savedInstanceState)
setContentView([Link].activity_updatedata)

val actionbar = supportActionBar


actionbar!!.title = "SQLite Update Data"
[Link](true)

edid=findViewById([Link])
edname=findViewById([Link])
edmob=findViewById([Link])

btnupdate=findViewById([Link])

[Link] {

val info_id=[Link]()
val info_name=[Link]()
val info_mob=[Link]()

if(info_id==""||info_name==""|| info_mob=="")
{
[Link](this,"Enter Data",Toast.LENGTH_LONG).show()
}
else
{
val result=[Link](info_id,info_name,info_mob)
if(result==true)
{
[Link](this,"Record
Updated",Toast.LENGTH_LONG).show()

}
else
{
[Link](this,"Record Not
BY : RAVI TALSANIA 16
SQLITE EXAMPLE

Updated",Toast.LENGTH_LONG).show()

}
}
}

}
override fun onSupportNavigateUp(): Boolean
{
onBackPressed()
return true
}
}

BY : RAVI TALSANIA 17
SQLITE EXAMPLE

(5) [Link]

package [Link]

import [Link]
import [Link]
import [Link]
import [Link]
import [Link]

class deletedata : AppCompatActivity()


{
internal val dbHelper=DatabaseHelper(this)
lateinit var edid:EditText
lateinit var btndelete:Button

override fun onCreate(savedInstanceState: Bundle?) {


[Link](savedInstanceState)
setContentView([Link].activity_deletedata)

val actionbar = supportActionBar


actionbar!!.title = "SQLite Delete Data"
[Link](true)

edid=findViewById([Link])
btndelete=findViewById([Link])

[Link] {

val info_id=[Link]()

if(info_id=="")
{
[Link](this,"Enter Data", Toast.LENGTH_LONG).show()
}
else
{
val result:Int=[Link](info_id)

if(result==1)
{
[Link](this,"Record
Deleted",Toast.LENGTH_LONG).show()

}
else
{
[Link](this,"Record Not
Deleted",Toast.LENGTH_LONG).show()

}
}

BY : RAVI TALSANIA 18
SQLITE EXAMPLE

}
override fun onSupportNavigateUp(): Boolean
{
onBackPressed()
return true
}
}

BY : RAVI TALSANIA 19
SQLITE EXAMPLE

(6) [Link]

package [Link]

import [Link]
import [Link]
import [Link]
import [Link]

class viewdata : AppCompatActivity()


{
internal var dbHelper=DatabaseHelper(this)

lateinit var txtdata:TextView

override fun onCreate(savedInstanceState: Bundle?) {


[Link](savedInstanceState)
setContentView([Link].activity_viewdata)

val actionbar = supportActionBar


actionbar!!.title = "SQLite View Data"
[Link](true)

txtdata=findViewById([Link])

val cur:Cursor= [Link]


val sb=StringBuffer()

if(cur!=null && [Link]>0)


{
while ([Link]())
{
[Link]("ID :"+[Link](0).toString()+" ")
[Link]("NAME :"+[Link](1).toString()+" ")
[Link]("MOBILE :"+[Link](2).toString()+" ")
[Link]("\n")
[Link]=[Link]()
}
}

}
override fun onSupportNavigateUp(): Boolean
{
onBackPressed()
return true
}

BY : RAVI TALSANIA 20

You might also like