SQL Program for name and roll
SQL Program for name and roll
Xml file
<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>
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;
@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);
}
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;
@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);
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;
}
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;
}