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

SQL CRUD Operations for Student Management

The document outlines an Android application that implements a basic SQL program for managing student records with Create, Read, Update, and Delete (CRUD) functionalities. It includes XML layout for user input and buttons, a DatabaseHelper class for database operations, and a MainActivity class to handle user interactions. The application allows users to add, view, update, and delete student information stored in a SQLite database.

Uploaded by

Yash Jain
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 views8 pages

SQL CRUD Operations for Student Management

The document outlines an Android application that implements a basic SQL program for managing student records with Create, Read, Update, and Delete (CRUD) functionalities. It includes XML layout for user input and buttons, a DatabaseHelper class for database operations, and a MainActivity class to handle user interactions. The application allows users to add, view, update, and delete student information stored in a SQLite database.

Uploaded by

Yash Jain
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

SQL program with Create , Update , Read and Delete

Xml

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


<ScrollView
xmlns:android="[Link]
android:layout_width="match_parent"
android:layout_height="match_parent">

<LinearLayout
android:orientation="vertical"
android:padding="16dp"
android:layout_width="match_parent"
android:layout_height="wrap_content">

<!-- Add student -->


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

<EditText
android:id="@+id/editTextRoll"
android:hint="Enter Roll"
android:inputType="number"
android:layout_width="match_parent"
android:layout_height="wrap_content" />

<Button
android:id="@+id/buttonSave"
android:text="Save Student"
android:layout_width="match_parent"
android:layout_height="wrap_content" />

<!-- Update student -->


<EditText
android:id="@+id/editTextId"
android:hint="Enter ID to Update"
android:inputType="number"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:layout_marginTop="16dp"/>

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

<EditText
android:id="@+id/editTextEditRoll"
android:hint="New Roll"
android:inputType="number"
android:layout_width="match_parent"
android:layout_height="wrap_content" />

<Button
android:id="@+id/buttonUpdate"
android:text="Update Student"
android:layout_width="match_parent"
android:layout_height="wrap_content" />

<!-- Delete student -->


<EditText
android:id="@+id/editTextDeleteId"
android:hint="Enter ID to Delete"
android:inputType="number"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:layout_marginTop="16dp"/>

<Button
android:id="@+id/buttonDelete"
android:text="Delete Student"
android:layout_width="match_parent"
android:layout_height="wrap_content" />

<!-- View students -->


<Button
android:id="@+id/buttonView"
android:text="View All Students"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:layout_marginTop="16dp"/>

<TextView
android:id="@+id/textViewResult"
android:textSize="16sp"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:paddingTop="16dp" />
</LinearLayout>
</ScrollView>

Database Helper

package [Link];

import [Link];
import [Link];
import [Link];
import [Link];
import [Link];

import [Link];

public class DatabaseHelper extends SQLiteOpenHelper


{

private static final String DATABASE_NAME =


"crashCourseDB";
private static final String TABLE_NAME =
"students";
private static final String COL_ID = "id";
private static final String COL_NAME = "name";
private static final String COL_ROLL = "roll";

public DatabaseHelper(@Nullable Context context) {


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

@Override
public void onCreate(SQLiteDatabase db) {
String createTable = "CREATE TABLE " +
TABLE_NAME + " (" +
COL_ID + " INTEGER PRIMARY KEY
AUTOINCREMENT, " +
COL_NAME + " TEXT, " +
COL_ROLL + " INTEGER)";
[Link](createTable);
}

@Override
public void onUpgrade(SQLiteDatabase db, int
oldVersion, int newVersion) {
[Link]("DROP TABLE IF EXISTS " +
TABLE_NAME);
onCreate(db);
}

public boolean insertStudents(String name, int


roll) {
SQLiteDatabase db =
[Link]();
ContentValues values = new ContentValues();
[Link](COL_NAME, name);
[Link](COL_ROLL, roll);
long result = [Link](TABLE_NAME, null,
values);
return result != -1;
}

public Cursor getAllStudents() {


SQLiteDatabase db =
[Link]();
return [Link]("SELECT * FROM " +
TABLE_NAME, null);
}

public boolean updateStudent(int id, String name,


int roll) {
SQLiteDatabase db =
[Link]();
ContentValues values = new ContentValues();
[Link](COL_NAME, name);
[Link](COL_ROLL, roll);
int result = [Link](TABLE_NAME, values,
COL_ID + "=?", new String[]{[Link](id)});
return result > 0;
}

public boolean deleteStudent(int id) {


SQLiteDatabase db =
[Link]();
int result = [Link](TABLE_NAME, COL_ID +
"=?", new String[]{[Link](id)});
return result > 0;
}
}

MainActivity

package [Link];

import [Link];
import [Link];
import [Link];
import [Link];
import [Link];
import [Link];
import [Link];
import [Link];

public class MainActivity extends AppCompatActivity {


EditText name, roll, editId, editName, editRoll, deleteId;
Button save, read, update, delete;
TextView display;
DatabaseHelper databaseHelper;

@Override
protected void onCreate(Bundle savedInstanceState) {
[Link](savedInstanceState);
setContentView([Link].activity_main);

name = findViewById([Link]);
roll = findViewById([Link]);
editId = findViewById([Link]);
editName = findViewById([Link]);
editRoll = findViewById([Link]);
deleteId = findViewById([Link]);

save = findViewById([Link]);
read = findViewById([Link]);
update = findViewById([Link]);
delete = findViewById([Link]);

display = findViewById([Link]);
databaseHelper = new DatabaseHelper(this);

[Link](new [Link]() {
@Override
public void onClick(View v) {
String userName = [Link]().toString();
int userRoll = [Link]([Link]().toString());

boolean inserted = [Link](userName, userRoll);


if (inserted) {
[Link]([Link], "Student Added",
Toast.LENGTH_SHORT).show();
[Link]("");
[Link]("");
} else {
[Link]([Link], "Error",
Toast.LENGTH_SHORT).show();
}
}
});

[Link](new [Link]() {
@Override
public void onClick(View v) {
Cursor cursor = [Link]();
StringBuilder data = new StringBuilder();
while ([Link]()) {
[Link]("ID : ").append([Link](0))
.append(" | Name : ").append([Link](1))
.append(" | Roll : ").append([Link](2)).append("\n");
}
[Link]([Link]());
[Link]();
}
});

[Link](new [Link]() {
@Override
public void onClick(View v) {
int id = [Link]([Link]().toString());
String newName = [Link]().toString();
int newRoll = [Link]([Link]().toString());

boolean updated = [Link](id, newName, newRoll);


if (updated) {
[Link]([Link], "Student Updated",
Toast.LENGTH_SHORT).show();
} else {
[Link]([Link], "Update Failed",
Toast.LENGTH_SHORT).show();
}
}
});

[Link](new [Link]() {
@Override
public void onClick(View v) {
int id = [Link]([Link]().toString());
boolean deleted = [Link](id);
if (deleted) {
[Link]([Link], "Student Deleted",
Toast.LENGTH_SHORT).show();
} else {
[Link]([Link], "Delete Failed",
Toast.LENGTH_SHORT).show();
}
}
});
}
}

You might also like