Practical No.
26
• Mainactivity.java
package com.example.pr13;
import android.os.Bundle;
import android.view.View;
import android.widget.EditText;
import android.widget.Button;
import android.widget.Toast;
import androidx.appcompat.app.AppCompatActivity;
public class MainActivity extends AppCompatActivity {
private EditText nameEditText, ageEditText;
private Button insertButton, deleteButton;
private MySQLiteHelper dbHelper;
@Override protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
nameEditText = findViewById(R.id.nameEditText);
ageEditText = findViewById(R.id.ageEditText);
insertButton = findViewById(R.id.insertButton);
deleteButton = findViewById(R.id.deleteButton);
dbHelper = MySQLiteHelper.getInstance(this); // Use singleton instance
insertButton.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View v) {
String name = nameEditText.getText().toString().trim();
String ageText = ageEditText.getText().toString().trim();
if (name.isEmpty() || ageText.isEmpty()) {
Toast.makeText(MainActivity.this, "Please enter all fields",
Toast.LENGTH_SHORT).show();
return;
}
try {
int age = Integer.parseInt(ageText);
new InsertDataAsyncTask(MainActivity.this, name, age).execute();
} catch (NumberFormatException e) {
Toast.makeText(MainActivity.this, "Invalid age format",
Toast.LENGTH_SHORT).show();
}
}
});
deleteButton.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View v) {
String name = nameEditText.getText().toString().trim();
if (name.isEmpty()) {
Toast.makeText(MainActivity.this, "Enter name to delete",
Toast.LENGTH_SHORT).show();
return;
}
new DeleteDataAsyncTask(MainActivity.this, name).execute();
}
});
}
@Override
protected void onDestroy() {
super.onDestroy();
dbHelper.closeDatabase(); // Close database when activity is destroyed
}
}
2.
• 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 for name input -->
<EditText
android:id="@+id/nameEditText"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:hint="Enter name"
android:inputType="text"
android:layout_marginTop="16dp" />
<!-- EditText for age input -->
<EditText
android:id="@+id/ageEditText"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:hint="Enter age"
android:inputType="number"
android:layout_marginTop="16dp" />
<!-- Button to insert data -->
<Button
android:id="@+id/insertButton"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="Insert Data"
android:layout_gravity="center"
android:layout_marginTop="16dp"/>
<!-- Button to delete data -->
<Button
android:id="@+id/deleteButton"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="Delete Data"
android:layout_gravity="center"
android:layout_marginTop="16dp"/>
</LinearLayout>
• InsertDataAsyncTask.java
• package com.example.pr13;
import android.os.AsyncTask;
import android.widget.Toast;
import android.content.Context;
public class InsertDataAsyncTask extends AsyncTask<Void, Void, Void> {
private Context context;
private String name;
private int age;
public InsertDataAsyncTask(Context context, String name, int age) {
this.context = context;
this.name = name;
this.age = age;
}
@Override
protected Void doInBackground(Void... voids) {
MySQLiteHelper dbHelper = MySQLiteHelper.getInstance(context);
dbHelper.insertData(name, age);
return null;
}
@Override
protected void onPostExecute(Void result) {
Toast.makeText(context, "Data inserted successfully",
Toast.LENGTH_SHORT).show();
}
}
• DeleteDataAsyncTask.java
package com.example.pr13;
import android.os.AsyncTask;
import android.widget.Toast;
import android.content.Context;
public class DeleteDataAsyncTask extends AsyncTask<Void, Void,
Boolean> {
private Context context;
private String name;
public DeleteDataAsyncTask(Context context, String name) {
this.context = context;
this.name = name;
}
@Override
protected Boolean doInBackground(Void... voids) {
MySQLiteHelper dbHelper =
MySQLiteHelper.getInstance(context); // Use singleton
return dbHelper.deleteData(name);
}
@Override
protected void onPostExecute(Boolean success) {
if (success) {
Toast.makeText(context, "Record deleted successfully",
Toast.LENGTH_SHORT).show();
} else {
Toast.makeText(context, "No record found with this
name", Toast.LENGTH_SHORT).show();
}
}
}
• MySQLiteHelper.java
package com.example.exp26sqldemo;
import android.content.ContentValues;
import android.content.Context;
import android.database.Cursor;
import android.database.sqlite.SQLiteDatabase;
import android.database.sqlite.SQLiteOpenHelper;
public class MySQLiteHelper extends SQLiteOpenHelper {
private static final String DATABASE_NAME = "mydatabase.db";
private static final int DATABASE_VERSION = 1;
public static final String TABLE_NAME = "users";
public static final String COLUMN_ID = "id";
public static final String COLUMN_NAME = "name";
public static final String COLUMN_AGE = "age";
private static MySQLiteHelper instance; // Singleton instance
private SQLiteDatabase db; // Database reference
private static final String TABLE_CREATE =
"CREATE TABLE " + TABLE_NAME + " (" +
COLUMN_ID + " INTEGER PRIMARY KEY AUTOINCREMENT, " +
COLUMN_NAME + " TEXT, " +
COLUMN_AGE + " INTEGER" +
");";
private MySQLiteHelper(Context context) {
super(context, DATABASE_NAME, null, DATABASE_VERSION);
db = this.getWritableDatabase(); // Open DB once
}
// Singleton pattern to prevent multiple instances
public static synchronized MySQLiteHelper getInstance(Context context) {
if (instance == null) {
instance = new MySQLiteHelper(context.getApplicationContext());
}
return instance;
}
@Override
public void onCreate(SQLiteDatabase db) {
db.execSQL(TABLE_CREATE);
}
@Override
public void onUpgrade(SQLiteDatabase db, int oldVersion, int newVersion)
{
db.execSQL("DROP TABLE IF EXISTS " + TABLE_NAME);
onCreate(db);
}
public boolean insertData(String name, int age) {
ContentValues values = new ContentValues();
values.put(COLUMN_NAME, name);
values.put(COLUMN_AGE, age);
long result = db.insert(TABLE_NAME, null, values);
return result != -1; // Returns true if insert successful
}
public boolean deleteData(String name) {
int rowsDeleted = db.delete(TABLE_NAME, COLUMN_NAME + "=?", new
String[]{name});
return rowsDeleted > 0; // Returns true if deletion was successful
}
public void closeDatabase() {
if (db != null && db.isOpen()) {
db.close(); // Close DB only when necessary
}
}
}
• Output