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

Practical 5

Practical 5.

Uploaded by

vhoratanvir1610
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)
4 views13 pages

Practical 5

Practical 5.

Uploaded by

vhoratanvir1610
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
You are on page 1/ 13

Mobile Application Development (202046712) 12202080501057

Practical – 5
Aim: Develop an android application that create, save, update, and
delete data in database.
activity_main.xml:
<?xml version="1.0" encoding="utf-8"?>
<LinearLayout
xmlns:android="http://schemas.android.com/apk/res/android"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:orientation="vertical"
android:padding="16dp">

<EditText
android:id="@+id/editName"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:hint="Name" />

<EditText
android:id="@+id/editEmail"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:hint="Email" />

GCET 1
Mobile Application Development (202046712) 12202080501057

<Button
android:id="@+id/btnAdd"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:text="Add User" />

<Button
android:id="@+id/btnView"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:text="View All Users" />

<Button
android:id="@+id/btnUpdate"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:text="Update User by ID" />

<Button
android:id="@+id/btnDelete"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:text="Delete User by ID" />
</LinearLayout>

GCET 2
Mobile Application Development (202046712) 12202080501057

DatabaseHelper.java:
package com.example.prac5;

import android.content.ContentValues;
import android.content.Context;
import android.database.Cursor;
import android.database.sqlite.*;

public class DatabaseHelper extends SQLiteOpenHelper {

public static final String DB_NAME = "Users.db";


public static final String TABLE_NAME = "user_table";
public static final String COL_1 = "ID";
public static final String COL_2 = "NAME";
public static final String COL_3 = "EMAIL";

public DatabaseHelper(Context context) {


super(context, DB_NAME, null, 1);
}

@Override
public void onCreate(SQLiteDatabase db) {
db.execSQL("CREATE TABLE " + TABLE_NAME +
" (ID INTEGER PRIMARY KEY AUTOINCREMENT, NAME TEXT, EMAIL
TEXT)");

GCET 3
Mobile Application Development (202046712) 12202080501057

@Override
public void onUpgrade(SQLiteDatabase db, int oldV, int newV) {
db.execSQL("DROP TABLE IF EXISTS " + TABLE_NAME);
onCreate(db);
}

public boolean insertData(String name, String email) {


SQLiteDatabase db = this.getWritableDatabase();
ContentValues cv = new ContentValues();
cv.put(COL_2, name);
cv.put(COL_3, email);
long result = db.insert(TABLE_NAME, null, cv);
return result != -1; // returns true if inserted
}

public Cursor getAllData() {


SQLiteDatabase db = this.getWritableDatabase();
return db.rawQuery("SELECT * FROM " + TABLE_NAME, null);
}

public boolean updateData(String id, String name, String email) {


SQLiteDatabase db = this.getWritableDatabase();

GCET 4
Mobile Application Development (202046712) 12202080501057

ContentValues cv = new ContentValues();


cv.put(COL_2, name);
cv.put(COL_3, email);
int rows = db.update(TABLE_NAME, cv, "ID=?", new String[]{id});
return rows > 0; // true if update successful
}

public Integer deleteData(String id)


{

SQLiteDatabase db = this.getWritableDatabase();
return db.delete(TABLE_NAME, "ID=?", new String[]{id});
}

GCET 5
Mobile Application Development (202046712) 12202080501057

MainActivity.java:
package com.example.prac5;

import android.database.Cursor;
import android.os.Bundle;
import android.view.View;
import android.widget.*;
import androidx.appcompat.app.AlertDialog;
import androidx.appcompat.app.AppCompatActivity;

public class MainActivity extends AppCompatActivity {

DatabaseHelper db;
EditText editName, editEmail;
Button btnAdd, btnView, btnUpdate, btnDelete;

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

db = new DatabaseHelper(this);
editName = findViewById(R.id.editName);
editEmail = findViewById(R.id.editEmail);

GCET 6
Mobile Application Development (202046712) 12202080501057

btnAdd = findViewById(R.id.btnAdd);
btnView = findViewById(R.id.btnView);
btnUpdate = findViewById(R.id.btnUpdate);
btnDelete = findViewById(R.id.btnDelete);

addUser();
viewUsers();
updateUser();
deleteUser();
}

public void addUser() {


btnAdd.setOnClickListener(v -> {
boolean inserted = db.insertData(
editName.getText().toString(),
editEmail.getText().toString());

Toast.makeText(this, inserted ? "User Added!" : "Error Adding User",


Toast.LENGTH_SHORT).show();
});
}

public void viewUsers() {


btnView.setOnClickListener(v -> {
Cursor res = db.getAllData();

GCET 7
Mobile Application Development (202046712) 12202080501057

if (res.getCount() == 0) {
showMessage("Oops", "No users found");
return;
}

StringBuilder buffer = new StringBuilder();


while (res.moveToNext()) {
buffer.append("ID: ").append(res.getString(0)).append("\n");
buffer.append("Name: ").append(res.getString(1)).append("\n");
buffer.append("Email: ").append(res.getString(2)).append("\n\n");
}

showMessage("All Users", buffer.toString());


});
}

public void updateUser() {


btnUpdate.setOnClickListener(v -> {
final EditText inputId = new EditText(this);
inputId.setHint("Enter ID to Update");

new AlertDialog.Builder(this)
.setTitle("Update User")
.setMessage("Enter ID of user to update")
.setView(inputId)

GCET 8
Mobile Application Development (202046712) 12202080501057

.setPositiveButton("Update", (dialog, which) -> {


boolean updated = db.updateData(
inputId.getText().toString(),
editName.getText().toString(),
editEmail.getText().toString());

Toast.makeText(this, updated ? "User Updated!" : "User Not


Found", Toast.LENGTH_SHORT).show();
})
.setNegativeButton("Cancel", null)
.show();
});
}

public void deleteUser() {


btnDelete.setOnClickListener(v -> {
final EditText inputId = new EditText(this);
inputId.setHint("Enter ID to Delete");

new AlertDialog.Builder(this)
.setTitle("Delete User")
.setMessage("Enter ID of user to delete")
.setView(inputId)
.setPositiveButton("Delete", (dialog, which) -> {
int deletedRows = db.deleteData(inputId.getText().toString());

GCET 9
Mobile Application Development (202046712) 12202080501057

Toast.makeText(this, deletedRows > 0 ? "User Deleted!" : "User


Not Found", Toast.LENGTH_SHORT).show();
})
.setNegativeButton("Cancel", null)
.show();
});
}

public void showMessage(String title, String msg) {


new AlertDialog.Builder(this)
.setTitle(title)
.setMessage(msg)
.setPositiveButton("OK", null)
.show();
}
}

GCET 10
Mobile Application Development (202046712) 12202080501057

Output:

Create/Add Users: View all users/saved users:

GCET 11
Mobile Application Development (202046712) 12202080501057

Update the users by ID:

GCET 12
Mobile Application Development (202046712) 12202080501057

Delete the users by ID:

GCET 13

You might also like