0% found this document useful (0 votes)
14 views2 pages

New Text Document

The document contains an Android layout XML and Java code for an employee update application. It features a TextView for displaying the title and a Button to update an employee record in a SQLite database. Upon clicking the button, it updates the employee's name and displays the updated record or a message if the record is not found.

Uploaded by

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

New Text Document

The document contains an Android layout XML and Java code for an employee update application. It features a TextView for displaying the title and a Button to update an employee record in a SQLite database. Upon clicking the button, it updates the employee's name and displays the updated record or a message if the record is not found.

Uploaded by

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

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

>
<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:tools="http://schemas.android.com/tools"
android:layout_width="match_parent"
android:layout_height="match_parent"
tools:context=".MainActivity">

<TextView
android:text="Update Employee E101"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_centerHorizontal="true"
android:textSize="20dp"
android:textColor="#000000"
android:layout_marginTop="40dp"
android:id="@+id/textView"/>

<Button
android:id="@+id/buttonUpdate"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:text="Update Record"
android:layout_below="@+id/textView"
android:layout_marginTop="30dp"/>

</RelativeLayout>

package com.example.employeeupdate;

import androidx.appcompat.app.AppCompatActivity;
import android.os.Bundle;
import android.content.Context;
import android.database.Cursor;
import android.database.sqlite.SQLiteDatabase;
import android.view.View;
import android.widget.Button;
import android.widget.Toast;

public class MainActivity extends AppCompatActivity {

SQLiteDatabase db;
Button buttonUpdate;

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

// Open or create the database


db = openOrCreateDatabase("EmployeeDB", Context.MODE_PRIVATE, null);

// Create table if not exists


db.execSQL("CREATE TABLE IF NOT EXISTS Employee(id VARCHAR PRIMARY KEY,
name VARCHAR);");

// Insert a sample employee for demonstration (E101, PQR)


db.execSQL("INSERT OR IGNORE INTO Employee(id, name) VALUES('E101',
'PQR');");

// Update button listener


buttonUpdate.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View v) {
// Update name where id is E101 and name is PQR
db.execSQL("UPDATE Employee SET name='XYZ' WHERE id='E101' AND
name='PQR';");

// Retrieve updated record


Cursor cursor = db.rawQuery("SELECT * FROM Employee WHERE
id='E101'", null);
if (cursor.moveToFirst()) {
String id = cursor.getString(0);
String name = cursor.getString(1);
Toast.makeText(getApplicationContext(), "Updated Record:\nID: "
+ id + "\nName: " + name, Toast.LENGTH_LONG).show();
} else {
Toast.makeText(getApplicationContext(), "Record not found",
Toast.LENGTH_SHORT).show();
}
cursor.close();
}
});
}
}

You might also like