0% found this document useful (0 votes)
69 views4 pages

Android Code 16.11.2021

This document contains code for an Android application that uses SQLite to store and retrieve user data from a database. It includes a MainActivity class with methods to insert, update, delete, and retrieve user data using a DatabaseHelper class. The DatabaseHelper class contains CRUD methods to interact with the SQLite database, including methods to insert, update, delete, and retrieve user records from a Userdetails table.

Uploaded by

Nyambura Kinyua
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)
69 views4 pages

Android Code 16.11.2021

This document contains code for an Android application that uses SQLite to store and retrieve user data from a database. It includes a MainActivity class with methods to insert, update, delete, and retrieve user data using a DatabaseHelper class. The DatabaseHelper class contains CRUD methods to interact with the SQLite database, including methods to insert, update, delete, and retrieve user records from a Userdetails table.

Uploaded by

Nyambura Kinyua
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/ 4

MainActivity.

java
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.Button;
import android.widget.EditText;
import android.widget.Toast;

public class MainActivity extends AppCompatActivity {


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

DatabaseHelper DB;

@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);

editTextTextPersonName=findViewById(R.id.editTextTextPersonName);
editTextTextPersonName2=findViewById(R.id.editTextTextPersonName2);
editTextTextPersonName3=findViewById(R.id.editTextTextPersonName3);

button=findViewById(R.id.button);
button2=findViewById(R.id.button2);
button3=findViewById(R.id.button3);
button4=findViewById(R.id.button4);

DB=new DatabaseHelper(this);

button.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View v) {
String
nametxt=editTextTextPersonName.getText().toString();
String
contacttxt=editTextTextPersonName2.getText().toString();
String
dobtxt=editTextTextPersonName3.getText().toString();

Boolean
checkinsertdata=DB.insertuserdata(nametxt,contacttxt,dobtxt);
if(checkinsertdata==true)
Toast.makeText(MainActivity.this,"New entry
inserted", Toast.LENGTH_SHORT).show();
else
Toast.makeText(MainActivity.this,"New Entry Not
Inserted",Toast.LENGTH_SHORT).show();
}
});

button2.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View v) {
String nametxt=editTextTextPersonName.getText().toString();
String
contacttxt=editTextTextPersonName2.getText().toString();
String dobtxt=editTextTextPersonName3.getText().toString();

Boolean
checkupdatedata=DB.updateuserdata(nametxt,contacttxt,dobtxt);
if(checkupdatedata==true)
Toast.makeText(MainActivity.this," Entry Updated",
Toast.LENGTH_SHORT).show();
else
Toast.makeText(MainActivity.this," Entry Not
Updated",Toast.LENGTH_SHORT).show();
}
});

button3.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View v) {
String nametxt=editTextTextPersonName.getText().toString();

Boolean checkdeletedata=DB.deleteuserdata(nametxt);
if(checkdeletedata==true)
Toast.makeText(MainActivity.this," Entry Deleted",
Toast.LENGTH_SHORT).show();
else
Toast.makeText(MainActivity.this," Entry Not
Deleted",Toast.LENGTH_SHORT).show();
}
});

button4.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View v) {
Cursor res=DB.getdata();
if (res.getCount()==0){//check if there are records to
display
Toast.makeText(MainActivity.this,"No Entry
Exists",Toast.LENGTH_SHORT).show();
return;
}

StringBuffer buffer=new StringBuffer();


while(res.moveToNext()){ //loop through the records
buffer.append("Name : "+res.getString(0)+"\n");
buffer.append("Contact : "+res.getString(1)+"\n");
buffer.append("Date of Birth : "+res.getString(2)+"\n\
n");
}

AlertDialog.Builder builder=new
AlertDialog.Builder(MainActivity.this);
builder.setCancelable(true);
builder.setTitle("User Entries");
builder.setMessage(buffer.toString());
builder.show();

}
});

}
}

DatabaseHelper.java

package com.example.sqliteapplication;

import android.content.ContentValues;
import android.content.Context;
import android.database.Cursor;
import android.database.sqlite.SQLiteDatabase;
import android.database.sqlite.SQLiteOpenHelper;

import androidx.annotation.Nullable;

public class DatabaseHelper extends SQLiteOpenHelper {

public DatabaseHelper(Context context) {

super(context, "Userdata.db", null, 1);


}

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

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

//CRUD OPERATIONS

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


SQLiteDatabase DB=this.getWritableDatabase();
ContentValues contentValues=new ContentValues();
contentValues.put("name",name);
contentValues.put("contact",contact);
contentValues.put("dob",dob);

long result=DB.insert("Userdetails",null,contentValues);
if(result==-1){
return false;
}else{
return true;
}

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


SQLiteDatabase DB=this.getWritableDatabase();
ContentValues contentValues=new ContentValues();
contentValues.put("contact",contact);
contentValues.put("dob",dob);

Cursor cursor=DB.rawQuery("Select * from Userdetails where name=?",


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

public Boolean deleteuserdata(String name){


SQLiteDatabase DB=this.getWritableDatabase();

Cursor cursor=DB.rawQuery("Select * from Userdetails where


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

public Cursor getdata(){


SQLiteDatabase DB=this.getWritableDatabase();

Cursor cursor=DB.rawQuery("Select * from Userdetails", null);


return cursor;

}
}

You might also like