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

Lab Task Connecting SQLite Database With Android

The document contains an XML layout for an Android application with multiple EditText fields for user input (name, contact, and date of birth) and buttons for inserting, updating, deleting, and viewing user data. The Java code implements the functionality of these buttons using a DatabaseHelper class to perform CRUD operations on a SQLite database. The app displays user entries in an AlertDialog when requested and provides feedback through Toast messages for various operations.

Uploaded by

remildanobiri9
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)
3 views6 pages

Lab Task Connecting SQLite Database With Android

The document contains an XML layout for an Android application with multiple EditText fields for user input (name, contact, and date of birth) and buttons for inserting, updating, deleting, and viewing user data. The Java code implements the functionality of these buttons using a DatabaseHelper class to perform CRUD operations on a SQLite database. The app displays user entries in an AlertDialog when requested and provides feedback through Toast messages for various operations.

Uploaded by

remildanobiri9
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

Xml file

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


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

<EditText
android:id="@+id/editTextTextPersonName"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_marginStart="98dp"
android:layout_marginLeft="98dp"
android:layout_marginTop="32dp"
android:layout_marginEnd="98dp"
android:layout_marginRight="98dp"
android:ems="10"
android:hint="Enter Name"
android:inputType="textPersonName"
app:layout_constraintEnd_toEndOf="parent"
app:layout_constraintStart_toStartOf="parent"
app:layout_constraintTop_toTopOf="parent" />

<EditText
android:id="@+id/editTextTextPersonName2"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_marginStart="98dp"
android:layout_marginLeft="98dp"
android:layout_marginTop="28dp"
android:layout_marginEnd="98dp"
android:layout_marginRight="98dp"
android:ems="10"
android:hint="Enter Tel Contact"
android:inputType="textPersonName"
app:layout_constraintEnd_toEndOf="parent"
app:layout_constraintStart_toStartOf="parent"
app:layout_constraintTop_toBottomOf="@+id/editTextTextPersonName"
/>

<EditText
android:id="@+id/editTextTextPersonName3"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_marginStart="98dp"
android:layout_marginLeft="98dp"
android:layout_marginTop="26dp"
android:layout_marginEnd="98dp"
android:layout_marginRight="98dp"
android:ems="10"
android:hint="Enter Date of birth"
android:inputType="textPersonName"
app:layout_constraintEnd_toEndOf="parent"
app:layout_constraintStart_toStartOf="parent"
app:layout_constraintTop_toBottomOf="@+id/editTextTextPersonName2"
/>

<Button
android:id="@+id/button"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_marginStart="16dp"
android:layout_marginLeft="16dp"
android:layout_marginTop="26dp"
android:text="INSERT"
app:layout_constraintEnd_toStartOf="@+id/button2"
app:layout_constraintStart_toStartOf="parent"
app:layout_constraintTop_toBottomOf="@+id/editTextTextPersonName3"
/>

<Button
android:id="@+id/button2"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_marginStart="24dp"
android:layout_marginLeft="24dp"
android:layout_marginTop="24dp"
android:layout_marginEnd="64dp"
android:layout_marginRight="64dp"
android:text="UPDATE"
app:layout_constraintEnd_toEndOf="parent"
app:layout_constraintStart_toEndOf="@+id/button"
app:layout_constraintTop_toBottomOf="@+id/editTextTextPersonName3"
/>

<Button
android:id="@+id/button3"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_marginStart="16dp"
android:layout_marginLeft="16dp"
android:layout_marginTop="17dp"
android:text="DELETE"
app:layout_constraintEnd_toStartOf="@+id/button4"
app:layout_constraintStart_toStartOf="parent"
app:layout_constraintTop_toBottomOf="@+id/button" />

<Button
android:id="@+id/button4"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_marginStart="40dp"
android:layout_marginLeft="40dp"
android:layout_marginTop="8dp"
android:layout_marginEnd="64dp"
android:layout_marginRight="64dp"
android:text="VIEW"
app:layout_constraintEnd_toEndOf="parent"
app:layout_constraintStart_toEndOf="@+id/button3"
app:layout_constraintTop_toBottomOf="@+id/button2" />
</[Link]>
Java File
package [Link];

import [Link];
import [Link];

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

public class MainActivity extends AppCompatActivity {


private EditText
editTextTextPersonName,editTextTextPersonName2,editTextTextPersonName3;
private Button button, button2, button3, button4;

DatabaseHelper DB; //object of class DatabaseHelper

@Override
protected void onCreate(Bundle savedInstanceState) {
[Link](savedInstanceState);
setContentView([Link].activity_main);

editTextTextPersonName=findViewById([Link]);
editTextTextPersonName2=findViewById([Link].editTextTextPersonName2);
editTextTextPersonName3=findViewById([Link].editTextTextPersonName3);

button=findViewById([Link]);
button2=findViewById([Link].button2);
button3=findViewById([Link].button3);
button4=findViewById([Link].button4);

DB=new DatabaseHelper(this);

[Link](new [Link]() {
@Override
public void onClick(View v) {
String
nametxt=[Link]().toString();
String
contacttxt=[Link]().toString();
String
dobtxt=[Link]().toString();

Boolean
checkinsertdata=[Link](nametxt,contacttxt,dobtxt);
if(checkinsertdata==true)
[Link]([Link],"New entry
inserted", Toast.LENGTH_SHORT).show();
else
[Link]([Link],"New Entry Not
Inserted",Toast.LENGTH_SHORT).show();
}
});
[Link](new [Link]() {
@Override
public void onClick(View v) {
String nametxt=[Link]().toString();
String
contacttxt=[Link]().toString();
String dobtxt=[Link]().toString();

Boolean
checkupdatedata=[Link](nametxt,contacttxt,dobtxt);
if(checkupdatedata==true)
[Link]([Link]," Entry Updated",
Toast.LENGTH_SHORT).show();
else
[Link]([Link]," Entry Not
Updated",Toast.LENGTH_SHORT).show();
}
});

[Link](new [Link]() {
@Override
public void onClick(View v) {
String nametxt=[Link]().toString();

Boolean checkdeletedata=[Link](nametxt);
if(checkdeletedata==true)
[Link]([Link]," Entry Deleted",
Toast.LENGTH_SHORT).show();
else
[Link]([Link]," Entry Not
Deleted",Toast.LENGTH_SHORT).show();
}
});

[Link](new [Link]() {
@Override
public void onClick(View v) {
Cursor res=[Link]();
if ([Link]()==0){//check if there are records to
display
[Link]([Link],"No Entry
Exists",Toast.LENGTH_SHORT).show();
return;
}

StringBuffer buffer=new StringBuffer();


while([Link]()){ //loop through the records
[Link]("Name : "+[Link](0)+"\n");
[Link]("Contact : "+[Link](1)+"\n");
[Link]("Date of Birth : "+[Link](2)+"\n\
n");
}

[Link] builder=new
[Link]([Link]);
[Link](true);
[Link]("User Entries");
[Link]([Link]());
[Link]();

}
});

}
}

Database Helper File


package [Link];

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

import [Link];

public class DatabaseHelper extends SQLiteOpenHelper {

public DatabaseHelper(Context context) {

super(context, "[Link]", null, 1);


}

@Override
public void onCreate(SQLiteDatabase DB) {
[Link]("Create Table Userdetails(name TEXT primary key, contact TEXT,
dob TEXT)");
}

@Override
public void onUpgrade(SQLiteDatabase DB, int oldVersion, int
newVersion) {
[Link]("drop Table if exists Userdetails");
}

//CRUD OPERATIONS

public Boolean insertuserdata(String name, String contact, String dob){


SQLiteDatabase DB=[Link](); //set the mode to
receive values
ContentValues contentValues=new ContentValues();
[Link]("name",name);
[Link]("contact",contact);
[Link]("dob",dob);

long result=[Link]("Userdetails",null,contentValues);
if(result==-1){
return false;
}else{
return true;
}
}

public Boolean updateuserdata(String name, String contact, String dob){


SQLiteDatabase DB=[Link]();
ContentValues contentValues=new ContentValues();
[Link]("contact",contact);
[Link]("dob",dob);

Cursor cursor=[Link]("Select * from Userdetails where name=?",


new String[]{name});
if([Link]()>0) {
long result = [Link]("Userdetails", contentValues, "name=?",
new String[]{name});
if (result == -1) {
return false;
} else {
return true;
}
}else{
return false;
}
}

public Boolean deleteuserdata(String name){


SQLiteDatabase DB=[Link]();

Cursor cursor=[Link]("Select * from Userdetails where


name=?", new String[]{name});
if([Link]()>0) {
long result = [Link]("Userdetails", "name=?", new String[]
{name});
if (result == -1) {
return false;
} else {
return true;
}
}else{
return false;
}
}

public Cursor getdata(){


SQLiteDatabase DB=[Link]();

Cursor cursor=[Link]("Select * from Userdetails", null);


return cursor;

}
}

You might also like