Database Operation Part 02 - Update and Deletion
Android Studio SQLite as a DBMS. Here we will discuss Update and Delete operation from CRUD operation of Database.
File Name
|
Code
|
XML Layout
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:gravity="center"
android:orientation="vertical"
tools:context=".MainActivity">
<TextView
android:id="@+id/register"
android:layout_margin="10dp"
android:padding="10dp"
android:gravity="center"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:background="@color/colorPrimaryDark"
android:text="Register Student" />
<TextView
android:id="@+id/show_detail"
android:layout_margin="10dp"
android:padding="10dp"
android:gravity="center"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:background="@color/colorPrimaryDark"
android:text="Show Student" />
<TextView
android:id="@+id/delete_Student"
android:layout_margin="10dp"
android:padding="10dp"
android:gravity="center"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:background="@color/colorPrimaryDark"
android:text="Delete Student" />
<TextView
android:id="@+id/update_student"
android:layout_margin="10dp"
android:padding="10dp"
android:gravity="center"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:background="@color/colorPrimaryDark"
android:text="Update Student" />
</LinearLayout>
|
Java Code
MainActivity.java
|
public class MainActivity extends AppCompatActivity {
TextView register,showDetail,delete,update;
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
register=findViewById(R.id.register);
showDetail=findViewById(R.id.show_detail);
delete=findViewById(R.id.delete_Student);
update=findViewById(R.id.update_student);
register.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View view) {
startActivity(new Intent(MainActivity.this,RegistrationAcvtivity.class));
}
});
showDetail.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View view) {
startActivity(new Intent(MainActivity.this,StudentDetailActivity.class));
}
});
delete.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View view) {
startActivity(new Intent(MainActivity.this,DeleteStudent.class));
}
});
update.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View view) {
startActivity(new Intent(MainActivity.this,UpdateStudent.class));
}
});
}
}
|
XML Layout
activity_delete_student.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"
tools:context=".RegistrationAcvtivity">
<EditText
android:id="@+id/name"
android:layout_margin="10dp"
android:padding="10dp"
android:gravity="center"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:hint="Name"
android:background="#f3f3f3" />
<EditText
android:id="@+id/fname"
android:layout_margin="10dp"
android:padding="10dp"
android:gravity="center"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:hint="Father Name"
android:background="#f3f3f3" />
<EditText
android:id="@+id/class_name"
android:layout_margin="10dp"
android:padding="10dp"
android:gravity="center"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:hint="Class Name"
android:background="#f3f3f3" />
<EditText
android:id="@+id/student_id"
android:layout_margin="10dp"
android:padding="10dp"
android:gravity="center"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:hint="ID"
android:background="#f3f3f3" />
<TextView
android:id="@+id/register"
android:layout_margin="10dp"
android:padding="10dp"
android:gravity="center"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:background="@color/colorPrimaryDark"
android:text="Delete" />
</LinearLayout>
|
Java Code
DeleteStudent .java
|
public class DeleteStudent extends AppCompatActivity {
EditText name,fatherName,className,studentID;
TextView register;
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_delete_student);
name=findViewById(R.id.name);
studentID=findViewById(R.id.student_id);
fatherName=findViewById(R.id.fname);
className=findViewById(R.id.class_name);
register=findViewById(R.id.register);
register.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View view) {
StudentDatabase db=new StudentDatabase(DeleteStudent.this);
long response=db.deleteStudent(studentID.getText().toString());
db.close();
if(response>0){
Toast.makeText(DeleteStudent.this,"Student Deleted Successfully",Toast.LENGTH_SHORT).show();
}
name.setText("");
fatherName.setText("");
className.setText("");
}
});
}
}
|
XMLCode
activity_update_student.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">
<EditText
android:id="@+id/name"
android:layout_margin="10dp"
android:padding="10dp"
android:gravity="center"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:hint="Name"
android:background="#f3f3f3" />
<EditText
android:id="@+id/fname"
android:layout_margin="10dp"
android:padding="10dp"
android:gravity="center"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:hint="Father Name"
android:background="#f3f3f3" />
<EditText
android:id="@+id/class_name"
android:layout_margin="10dp"
android:padding="10dp"
android:gravity="center"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:hint="Class Name"
android:background="#f3f3f3" />
<EditText
android:id="@+id/student_id"
android:layout_margin="10dp"
android:padding="10dp"
android:gravity="center"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:hint="ID"
android:background="#f3f3f3" />
<TextView
android:id="@+id/register"
android:layout_margin="10dp"
android:padding="10dp"
android:gravity="center"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:background="@color/colorPrimaryDark"
android:text="Update" />
</LinearLayout>
|
Java Code
UpdateStudent .java
|
public class UpdateStudent extends AppCompatActivity {
EditText name,fatherName,className,studentID;
TextView register;
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_update_student);
name=findViewById(R.id.name);
studentID=findViewById(R.id.student_id);
fatherName=findViewById(R.id.fname);
className=findViewById(R.id.class_name);
register=findViewById(R.id.register);
register.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View view) {
StudentDatabase db=new StudentDatabase(UpdateStudent.this);
long response=db.updateStudent(studentID.getText().toString(),name.getText().toString(),fatherName.getText().toString(),className.getText().toString());
db.close();
if(response>0){
Toast.makeText(UpdateStudent.this,"Student Updated Successfully",Toast.LENGTH_SHORT).show();
}
studentID.setText("");
name.setText("");
fatherName.setText("");
className.setText("");
}
});
}
}
|
Java Code
StudentDatabase.java
|
public class StudentDatabase extends SQLiteOpenHelper {
public StudentDatabase(Context context) {
super(context, "StudentDB", null, 1);
}
@Override
public void onCreate(SQLiteDatabase sqLiteDatabase) {
String studentTBL = "CREATE TABLE Student (Id INTEGER PRIMARY KEY AUTOINCREMENT,firstName TEXT,fatherName TEXT,className TEXT)";
sqLiteDatabase.execSQL(studentTBL);
}
@Override
public void onUpgrade(SQLiteDatabase sqLiteDatabase, int i, int i1) {
}
long registerStudent(String firstName, String fatherName, String className) {
SQLiteDatabase sqLiteDatabase = this.getReadableDatabase();
ContentValues contentValues = new ContentValues();
contentValues.put("firstName", firstName);
contentValues.put("fatherName", fatherName);
contentValues.put("className", className);
return sqLiteDatabase.insert("Student", null, contentValues);
}
String getStudentDetail() {
SQLiteDatabase sqLiteDatabase=this.getReadableDatabase();
String query="SELECT * FROM Student";
Cursor cursor=sqLiteDatabase.rawQuery(query,null);
String response="";
if(cursor.moveToFirst()){
do{
response+=cursor.getInt(0)+" : "+cursor.getString(1)+" : "+cursor.getString(2)+" : "+cursor.getString(3)+"\n";
}while (cursor.moveToNext());
}
cursor.close();
return response;
}
long deleteStudent(String id){
SQLiteDatabase sqLiteDatabase=this.getReadableDatabase();
return sqLiteDatabase.delete("Student", "Id=" + id,null);
}
long updateStudent(String id, String name,String fatherName,String className){
SQLiteDatabase sqLiteDatabase=this.getReadableDatabase();
ContentValues contentValues=new ContentValues();
contentValues.put("firstName", name);
contentValues.put("fatherName", fatherName);
contentValues.put("className", className);
return sqLiteDatabase.update("Student",contentValues,"Id = ?",new String[]{id});
}
}
|
Comments
Post a Comment