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

Android Practical.12

Uploaded by

Ritesh Kumar
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)
14 views6 pages

Android Practical.12

Uploaded by

Ritesh Kumar
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/ 6

PRACTICAL.

12
Create an application to insert, update and delete a record from the database.
Activitymain.xml

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


<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:padding="16dp">

<EditText
android:id="@+id/editTextName"
android:layout_width="match_parent"
android:layout_height="wrap_content"

android:hint="Name"/>

<Button
android:id="@+id/buttonInsert"
android:layout_width="wrap_content"

android:layout_height="wrap_content"
android:layout_below="@id/editTextName"
android:text="Insert"/>

<Button

android:id="@+id/buttonUpdate"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_below="@id/buttonInsert"
android:text="Update"/>
<Button
android:id="@+id/buttonDelete"
android:layout_width="wrap_content"

android:layout_height="wrap_content"
android:layout_below="@id/buttonUpdate"
android:text="Delete"/>

</RelativeLayout>

Mainactivity.java
import android.content.ContentValues;
import android.database.Cursor;
import android.database.sqlite.SQLiteDatabase;

import android.os.Bundle;
import android.view.View;
import android.widget.Button;
import android.widget.EditText;
import android.widget.Toast;

import androidx.appcompat.app.AppCompatActivity;

public class MainActivity extends AppCompatActivity {

private EditText editTextName;

private Button buttonInsert, buttonUpdate, buttonDelete;


private SQLiteDatabase database;

@Override
protected void onCreate(Bundle savedInstanceState) {

super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);

// Initialize views

editTextName = findViewById(R.id.editTextName);
buttonInsert = findViewById(R.id.buttonInsert);
buttonUpdate = findViewById(R.id.buttonUpdate);
buttonDelete = findViewById(R.id.buttonDelete);

// Create or open the database


database = openOrCreateDatabase("MyDatabase", MODE_PRIVATE, null);
createTable();

buttonInsert.setOnClickListener(new View.OnClickListener() {

@Override
public void onClick(View v) {
insertRecord();
}
});

buttonUpdate.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View v) {
updateRecord();

}
});

buttonDelete.setOnClickListener(new View.OnClickListener() {
@Override

public void onClick(View v) {


deleteRecord();
}
});

private void createTable() {


// Create table if not exists
database.execSQL("CREATE TABLE IF NOT EXISTS MyTable (id INTEGER PRIMARY KEY
AUTOINCREMENT, name TEXT)");
}

private void insertRecord() {


String name = editTextName.getText().toString().trim();
if (name.isEmpty()) {
Toast.makeText(this, "Please enter a name", Toast.LENGTH_SHORT).show();

return;
}
ContentValues values = new ContentValues();
values.put("name", name);
long result = database.insert("MyTable", null, values);

if (result != -1) {
Toast.makeText(this, "Record inserted successfully", Toast.LENGTH_SHORT).show();
} else {
Toast.makeText(this, "Failed to insert record", Toast.LENGTH_SHORT).show();
}

private void updateRecord() {


String name = editTextName.getText().toString().trim();
if (name.isEmpty()) {
Toast.makeText(this, "Please enter a name", Toast.LENGTH_SHORT).show();
return;
}

ContentValues values = new ContentValues();


values.put("name", name);
int rowsAffected = database.update("MyTable", values, null, null);
if (rowsAffected > 0) {
Toast.makeText(this, "Record updated successfully", Toast.LENGTH_SHORT).show();

} else {
Toast.makeText(this, "Failed to update record", Toast.LENGTH_SHORT).show();
}
}

private void deleteRecord() {


int rowsAffected = database.delete("MyTable", null, null);
if (rowsAffected > 0) {
Toast.makeText(this, "Record deleted successfully", Toast.LENGTH_SHORT).show();
} else {

Toast.makeText(this, "Failed to delete record", Toast.LENGTH_SHORT).show();


}
}

@Override

protected void onDestroy() {


super.onDestroy();
// Close the database
if (database != null) {
database.close();

}
}
}

You might also like