0% found this document useful (0 votes)
35 views13 pages

Android 18

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

Android 18

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

Practical No.

18
Aim: Develop a program to perform (Search and Update)
database operations using SQLite Database.
XML Code:
<?xml version="1.0" encoding="utf-8"?>
<RelativeLayout
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:padding="10dp"
android:gravity="center"
android:background="@drawable/background_color"
tools:context=".MainActivity">
<TextView
android:id="@+id/texƫtle"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:text=" Enter the Details "
android:textAlignment="center"
android:textStyle="bold"
android:textSize="24dp"
android:layout_marginTop="20dp"
/>
<EditText
android:id="@+id/name"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:hint="Name"
android:textColor="@color/white"
android:textColorHint="@color/white"
android:layout_below="@+id/textitle"
android:inputType="textPersonName"/>
<EditText
android:id="@+id/contact"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:hint="Contact"
android:textColor="@color/white"
android:textColorHint="@color/white"
android:layout_below="@+id/name"
android:inputType="number"/>
<EditText
android:id="@+id/dob"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:hint="Date of Birth"
android:textColor="@color/white"
android:textColorHint="@color/white"
android:layout_below="@+id/contact"
android:inputType="number"/>
<Button
android:id="@+id/btnInsert"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:textSize="22dp"
android:textColor="@color/white"
android:text="New Data"
android:backgroundTint="#2196F3"
android:layout_marginTop="30dp"
android:layout_below="@+id/dob"/>
<Button
android:id="@+id/btnUpdate"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:textSize="22dp"
android:text="Update Data"
android:backgroundTint="#2196F3"
android:layout_below="@+id/btnInsert"/>
<Button
android:id="@+id/btnDelete"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:textSize="22dp"
android:backgroundTint="#2196F3"
android:text="Delete Existing Data"
android:layout_below="@+id/btnUpdate"/>
<BuƩon android:id="@+id/btnView"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:textSize="22dp"
android:backgroundTint="#2196F3"
android:text="View Data"
android:layout_below="@+id/btnDelete"/>
<Button
android:id="@+id/btnSearch"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:textSize="22dp"
android:backgroundTint="#2196F3"
android:text="Search Data"
android:layout_below="@id/btnView"
/>
</RelativeLayout>
Java Code:
package com.example.sqliteapplication;
import androidx.appcompat.app.AlertDialog;
import androidx.appcompat.app.AppCompatActivity;
import android.database.Cursor;
import android.os.Bundle;
import android.view.View;
import android.widget.BuƩon;
import android.widget.EditText
import android.widget.Toast;
public class MainActivity extends AppCompatActivity {
EditText name,contact,dob;
BuƩon insert, update, delete,view,search;
DBHelper DB;
@Override
protected void onCreate(Bundle
savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.acƟvity_main);
name = findViewById(R.id.name);
contact = findViewById(R.id.contact);
dob = findViewById(R.id.dob);
insert = findViewById(R.id.btnInsert);
update = findViewById(R.id.btnUpdate);
delete = findViewById(R.id.btnDelete);
view = findViewById(R.id.btnView);
DB =new DBHelper(this);
insert.setOnClickListener(new
View.OnClickListener() {
@Override
public void onClick(View v) {
String nameTXT =
name.getText().toString().trim();
String contactTXT =
contact.getText().toString().trim();
String dobTXT =
dob.getText().toString().trim();
Boolean checkinsertdata =
DB.insertuserdata(nameTXT,contactTXT,dobTXT);
if (checkinsertdata == true)
{
Toast.makeText(MainAcƟvity.this, "New
Entry Inserted", Toast.LENGTH_SHORT).show();
}
else {
Toast.makeText(MainAcƟvity.this, "New
Entry is not Inserted ", Toast.LENGTH_SHORT).show();
}
}
});
update.setOnClickListener(new
View.OnClickListener() {
@Override
public void onClick(View v) {
String nameTXT =
name.getText().toString().trim();
String contactTXT =
contact.getText().toString().trim();
String dobTXT =
dob.getText().toString().trim();
Boolean checkupdatedata =
DB.updateuserdata(nameTXT,contactTXT,dobTXT);
if (checkupdatedata == true)
{
Toast.makeText(MainActivity.this, " Entry
Upated", Toast.LENGTH_SHORT).show();
}
else {
Toast.makeText(MainActivity.this, "Entry is
not Upadte ", Toast.LENGTH_SHORT).show();
} } });
delete.setOnClickListener(new
View.OnClickListener() {
@Override
public void onClick(View v) {
String nameTXT =
name.getText().toString().trim();
Boolean checkdeletedata =
DB.deleteuserdata(nameTXT);
if (checkdeletedata == true)
{
Toast.makeText(MainActivity.this, " Entry
deleted", Toast.LENGTH_SHORT).show();
}
else {
Toast.makeText(MainActivity.this, "Entry is
not deleted ", Toast.LENGTH_SHORT).show();
} } });
view.setOnClickListener(new
View.OnClickListener() {
@Override
public void onClick(View v) {
Cursor res = DB.getdata();
if(res.getCount() == 0)
{
Toast.makeText(MainActivity.this, "No
entry exist", Toast.LENGTH_SHORT).show();
return;
}
StringBuffer buffer = new StringBuffer();
while(res.moveToNext())
{
buffer.append("Name:"+res.getString(0)+"\n");

buffer.append("Contact:"+res.getString(1)+"\n");

buffer.append("Dob:"+res.getString(2)+"\n\n");
}
AlertDialog.Builder builder = new
AlertDialog.Builder(MainActivity.this);
builder.setCancelable(true);
builder.setTitle("User Record");
builder.setMessage(buffer.toString());
builder.show();
}
});
search = findViewById(R.id.btnSearch);
search.setOnClickListener(new
View.OnClickListener() {
@Override
public void onClick(View v) {
String searchName =
name.getText().toString().trim();
if (!searchName.isEmpty()) {
Cursor searchData =
DB.searchData(searchName);
if (searchData != null &&
searchData.getCount() > 0) {
StringBuffer buffer = new StringBuffer();
while (searchData.moveToNext()) {
buffer.append("Name:" +
searchData.getString(0) + "\n");
buffer.append("Contact:" +
searchData.getString(1) + "\n");
+ buffer.append("Dob:" +
searchData.getString(2) + "\n\n");
}
AlertDialog.Builder builder = new
AlertDialog.Builder(MainActivity.this);
builder.setCancelable(true);
builder.setTitle("Search Result");
builder.setMessage(buffer.toString());
builder.show();
} else {
Toast.makeText(MainActivity.this, "No
entry found for the provided name.",
Toast.LENGTH_SHORT).show();
}
} else {
Toast.makeText(MainActivity.this, "Please
enter a name to search.",
Toast.LENGTH_SHORT).show();
} } }); }}

Output:
1) 2)

You might also like