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

Practical No 26

The document outlines an Android application that allows users to manage a database of names using a simple user interface. It includes an XML layout for the main activity with input fields and buttons for adding, showing, updating, and deleting names, as well as a Java class for database operations. The DatabaseHelper class handles the creation, updating, and management of the SQLite database for storing names.

Uploaded by

Samiksha Bhosale
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)
3 views5 pages

Practical No 26

The document outlines an Android application that allows users to manage a database of names using a simple user interface. It includes an XML layout for the main activity with input fields and buttons for adding, showing, updating, and deleting names, as well as a Java class for database operations. The DatabaseHelper class handles the creation, updating, and management of the SQLite database for storing names.

Uploaded by

Samiksha Bhosale
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/ 5

Practical no.

26

Activity_main.xml

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


<LinearLayout 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"
android:orientation="vertical"
tools:context=".MainActivity">
<TextView
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:text="DYP"/>
<EditText
android:id="@+id/editTextName"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:hint="Enter name"/>
<Button
android:id="@+id/buttonAdd"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="Add"/>
<Button
android:id="@+id/buttonshow"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="Show"/>
<EditText
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:id="@+id/curr"
android:ems="10"
android:hint="Enter current Name"/>
<EditText
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:id="@+id/newname"
android:ems="10"
android:hint="Enter new name"/>
<Button
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:id="@+id/update"
android:text="Update"/>
<EditText
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:id="@+id/eddelete"
android:ems="10"
android:hint="Enter name for delete record"/>
<Button
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="Delete"
android:id="@+id/delete"/>
</LinearLayout>

DatabaseHelper.java

package com.example.db;
import android.content.ContentValues;
import android.content.Context;
import android.database.Cursor;
import android.database.sqlite.SQLiteDatabase;
import android.database.sqlite.SQLiteOpenHelper;
import android.widget.Toast;
public class DatabaseHelper extends SQLiteOpenHelper {
private static final int DATABASE_VERSION = 1;
private static final String DATABASE_NAME = "NamesDB";
private static final String TABLE_NAMES = "Names";
private static final String KEY_ID = "id";
private static final String KEY_NAME = "name";
public DatabaseHelper(Context context) {
super(context, DATABASE_NAME, null, DATABASE_VERSION);
}
@Override
public void onCreate(SQLiteDatabase db) {
String CREATE_NAMES_TABLE = "CREATE TABLE " + TABLE_NAMES + "("
+ KEY_ID + " INTEGER PRIMARY KEY," + KEY_NAME + " TEXT" + ")";
db.execSQL(CREATE_NAMES_TABLE);
}
@Override
public void onUpgrade(SQLiteDatabase db, int oldVersion, int newVersion) {
db.execSQL("DROP TABLE IF EXISTS " + TABLE_NAMES);
onCreate(db);
}
public void addName(String name) {
SQLiteDatabase db = this.getWritableDatabase();
ContentValues values = new ContentValues();
values.put(KEY_NAME, name);
db.insert(TABLE_NAMES, null, values);
db.close();
}
public String getAllNames() {
StringBuilder sb=new StringBuilder();
String selectQuery = "SELECT * FROM " + TABLE_NAMES;
SQLiteDatabase db = this.getWritableDatabase();
Cursor cursor = db.rawQuery(selectQuery, null);
if (cursor.moveToFirst()) {
do {
sb.append("Name-"+cursor.getString(1)+"\n");
} while (cursor.moveToNext());
}
cursor.close();

return sb.toString();
}
public void updateName(String old, String newName) {
SQLiteDatabase db = this.getWritableDatabase();
ContentValues values = new ContentValues();
values.put(KEY_NAME, newName);
db.update(TABLE_NAMES, values, KEY_NAME + " = ?", new String[]{String.valueOf(old)});
db.close();
}
public void deleteName(String old) {
SQLiteDatabase db = this.getWritableDatabase();
db.delete(TABLE_NAMES, KEY_NAME + " = ?", new String[]{String.valueOf(old)});
db.close();
}
}

MainActivity.java

package com.example.db;
import androidx.appcompat.app.AppCompatActivity
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 {
EditText editTextName,curr,newname,eddelete;
Button buttonAdd,showbut,update,delete;
DatabaseHelper db;
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
editTextName = findViewById(R.id.editTextName);
buttonAdd = findViewById(R.id.buttonAdd);
showbut=findViewById(R.id.buttonshow);
curr=(EditText)findViewById(R.id.curr);
newname=(EditText)findViewById(R.id.newname);
update=(Button)findViewById(R.id.update);
eddelete=(EditText)findViewById(R.id.eddelete);
delete=(Button)findViewById(R.id.delete);
db = new DatabaseHelper(this);
buttonAdd.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View v) {
String name = editTextName.getText().toString().trim();
if (!name.isEmpty()) {
db.addName(name);
Toast.makeText(MainActivity.this, "Added", Toast.LENGTH_SHORT).show();
editTextName.setText("");

} else {
Toast.makeText(MainActivity.this, "Please enter a name", Toast.LENGTH_SHORT).show();
}
}
});
showbut.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View v) {
String s=db.getAllNames();
Toast.makeText(MainActivity.this, s, Toast.LENGTH_SHORT).show();
}
});
update.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View v) {
String old=curr.getText().toString();
String new1=newname.getText().toString();
db.updateName(old,new1);
}
});

delete.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View v) {
String old=eddelete.getText().toString();
db.deleteName(old);

}
});

}}
output:

You might also like