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

SQL Program for name and roll

The document outlines an Android application that allows users to input student names and roll numbers, which are then stored in a SQLite database. It includes an XML layout for the user interface, a DatabaseHelper class for database operations, and a MainActivity class to handle user interactions. Users can save student data and view all stored records within the app.

Uploaded by

pareshloq
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 views4 pages

SQL Program for name and roll

The document outlines an Android application that allows users to input student names and roll numbers, which are then stored in a SQLite database. It includes an XML layout for the user interface, a DatabaseHelper class for database operations, and a MainActivity class to handle user interactions. Users can save student data and view all stored records within the app.

Uploaded by

pareshloq
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/ 4

SQL Program for ( name roll )

Xml file

<?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
android:id="@+id/editTextName"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:hint="Enter Name" />

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

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

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

<TextView
android:id="@+id/textViewResult"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:text="Stored Data will appear here"
android:padding="10dp"
android:textSize="16sp"/>
</LinearLayout>

Database helper class

package com.example.mydbdemo;
import android.content.ContentValues;
import android.content.Context;
import android.database.Cursor;
import android.database.sqlite.SQLiteDatabase;
import android.database.sqlite.SQLiteOpenHelper;

public class DatabaseHelper extends SQLiteOpenHelper {

private static final String DATABASE_NAME = "StudentDB";


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(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)";
db.execSQL(createTable);
}

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

public boolean insertStudent(String name, int roll) {


SQLiteDatabase db = this.getWritableDatabase();
ContentValues values = new ContentValues();
values.put(COL_NAME, name);
values.put(COL_ROLL, roll);

long result = db.insert(TABLE_NAME, null, values);


return result != -1; // returns true if inserted successfully
}

public Cursor getAllStudents() {


SQLiteDatabase db = this.getReadableDatabase();
return db.rawQuery("SELECT * FROM " + TABLE_NAME, null);
}
}

Mainactivity
package com.example.mydbdemo;

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

import androidx.appcompat.app.AppCompatActivity;

public class MainActivity extends AppCompatActivity {

EditText editTextName, editTextRoll;


Button buttonSave, buttonView;
TextView textViewResult;
DatabaseHelper databaseHelper;

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

editTextName = findViewById(R.id.editTextName);
editTextRoll = findViewById(R.id.editTextRoll);
buttonSave = findViewById(R.id.buttonSave);
buttonView = findViewById(R.id.buttonView);
textViewResult = findViewById(R.id.textViewResult);

databaseHelper = new DatabaseHelper(this);

buttonSave.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View v) {
String name = editTextName.getText().toString();
String rollStr = editTextRoll.getText().toString();

if (name.isEmpty() || rollStr.isEmpty()) {
Toast.makeText(MainActivity.this, "Please fill all fields", Toast.LENGTH_SHORT).show();
return;
}

int roll = Integer.parseInt(rollStr);


boolean inserted = databaseHelper.insertStudent(name, roll);

if (inserted) {
Toast.makeText(MainActivity.this, "Student Added!", Toast.LENGTH_SHORT).show();
editTextName.setText("");
editTextRoll.setText("");
} else {
Toast.makeText(MainActivity.this, "Error Adding Student",
Toast.LENGTH_SHORT).show();
}
}
});

buttonView.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View v) {
Cursor cursor = databaseHelper.getAllStudents();
if (cursor.getCount() == 0) {
textViewResult.setText("No records found.");
return;
}

StringBuilder data = new StringBuilder();


while (cursor.moveToNext()) {
data.append("ID: ").append(cursor.getInt(0))
.append(", Name: ").append(cursor.getString(1))
.append(", Roll: ").append(cursor.getInt(2))
.append("\n");
}
textViewResult.setText(data.toString());
cursor.close();
}
});
}
}

You might also like