main activity file (https://www.codeproject.com/Articles/783073/A-Simple-Android-SQLite-Example)
package com.example.viraj.studentmanagementsystem;
import android.content.Context;
import android.database.Cursor;
import android.database.sqlite.SQLiteDatabase;
import android.database.sqlite.SQLiteDatabaseLockedException;
import android.support.v7.app.AlertDialog;
import android.support.v7.app.AlertDialog.Builder;
import android.support.v7.app.AppCompatActivity;
import android.os.Bundle;
import android.view.View;
import android.widget.Button;
import android.widget.EditText;
public class MainActivity extends AppCompatActivity implements View.OnClickListener {
EditText ename, erollnumber, emarks;
Button btnAdd, btnDelet, btnModify, btnView, btnViewall, btnShowinfo;
SQLiteDatabase db;
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
// Initializing controls
ename = (EditText) findViewById(R.id.id_name);
erollnumber = (EditText) findViewById(R.id.id_rollno);
emarks = (EditText) findViewById(R.id.id_marks);
btnAdd = (Button) findViewById(R.id.id_add_button);
btnDelet = (Button) findViewById(R.id.id_delet_button);
btnModify = (Button) findViewById(R.id.id_modifi_button);
btnView = (Button) findViewById(R.id.id_view_button);
btnViewall = (Button) findViewById(R.id.id_viewallbutton);
btnShowinfo = (Button) findViewById(R.id.id_show_button);
// Registering event handlers
btnAdd.setOnClickListener(this);
btnModify.setOnClickListener(this);
btnDelet.setOnClickListener(this);
btnView.setOnClickListener(this);
btnViewall.setOnClickListener(this);
btnShowinfo.setOnClickListener(this);
db = openOrCreateDatabase("StudentDB", Context.MODE_PRIVATE, null);
db.execSQL("CREATE TABLE IF NOT EXISTS student(rollno VARCHAR,name VARCHAR,marks VARCHAR);");
}
@Override
public void onClick(View v) {
// Adding a record
if (v == btnAdd) {// Checking empty fields
if (ename.getText().toString().trim().length() == 0 ||
erollnumber.getText().toString().trim().length() == 0 ||
emarks.getText().toString().trim().length() == 0) {
showMessage("ERROR", "PLZ PROVIDE ALL THE RECORD");
return;
}
// Inserting record
db.execSQL("INSERT INTO student VALUES('" + ename.getText() + "','" + erollnumber.getText() + "','" + emarks.getText() + "')");
showMessage("SUCESSFULLY", "RECORD ADDED SUCESSFULLY");
clearText();
}
if (v==btnDelet)
{
//check for emptu record
if (erollnumber.getText().toString().trim().length()==0)
{
showMessage("ERROR","PLZ ENTER ROLL NO" );
return;
}
// Searching roll number
Cursor c=db.rawQuery("SELECT * FROM student WHERE rollno='"+erollnumber.getText()+"'", null);
if(c.moveToFirst())
{
// Deleting record if found
db.execSQL("DELETE FROM student WHERE rollno='"+erollnumber.getText()+"'");
showMessage("Success", "Record Deleted");
}
else
{
showMessage("Error", "Invalid Rollno");
}
clearText();
}
// Modifying a record
if(v==btnModify)
{
// Checking empty roll number
if(erollnumber.getText().toString().trim().length()==0)
{
showMessage("Error", "Please enter Rollno");
return;
}
// Searching roll number
Cursor c=db.rawQuery("SELECT * FROM student WHERE rollno='"+erollnumber.getText()+"'", null);
if(c.moveToFirst())
{
// Modifying record if found
db.execSQL("UPDATE student SET name='"+ename.getText()+"',marks='"+erollnumber.getText()+
"' WHERE rollno='"+erollnumber.getText()+"'");
showMessage("Success", "Record Modified");
}
else
{
showMessage("Error", "Invalid Rollno");
}
clearText();
}
// Viewing a record
if(v==btnView)
{
// Checking empty roll number
if(erollnumber.getText().toString().trim().length()==0)
{
showMessage("Error", "Please enter Rollno");
return;
}
// Searching roll number
Cursor c=db.rawQuery("SELECT * FROM student WHERE rollno='"+erollnumber.getText()+"'", null);
if(c.moveToFirst())
{
// Displaying record if found
ename.setText(c.getString(1));
emarks.setText(c.getString(2));
}
else
{
showMessage("Error", "Invalid Rollno");
clearText();
}
}
// Viewing all records
if(v==btnViewall)
{
// Retrieving all records
Cursor c=db.rawQuery("SELECT * FROM student", null);
// Checking if no records found
if(c.getCount()==0)
{
showMessage("Error", "No records found");
return;
}
// Appending records to a string buffer
StringBuffer buffer=new StringBuffer();
while(c.moveToNext())
{
buffer.append("Rollno: "+c.getString(0)+"\n");
buffer.append("Name: "+c.getString(1)+"\n");
buffer.append("Marks: "+c.getString(2)+"\n\n");
}
// Displaying all records
showMessage("Student Details", buffer.toString());
}
// Displaying info
if(v==btnShowinfo)
{
showMessage("THIS LEARNING APP CREATED FOR LEARNING PURPOSE ONLY", "WRITTEN AGAIN FOR MY SELF HELP https://www.codeproject.com/Articles/783073/A-Simple-Android-SQLite-Example");
}
}
private void showMessage(String title, String message) {
Builder builder = new Builder(this);
builder.setCancelable(true);
builder.setTitle(title);
builder.setMessage(message);
builder.show();
}
public void clearText() {
ename.setText("");
erollnumber.setText("");
emarks.setText("");
erollnumber.requestFocus();
}
}
package com.example.viraj.studentmanagementsystem;
import android.content.Context;
import android.database.Cursor;
import android.database.sqlite.SQLiteDatabase;
import android.database.sqlite.SQLiteDatabaseLockedException;
import android.support.v7.app.AlertDialog;
import android.support.v7.app.AlertDialog.Builder;
import android.support.v7.app.AppCompatActivity;
import android.os.Bundle;
import android.view.View;
import android.widget.Button;
import android.widget.EditText;
public class MainActivity extends AppCompatActivity implements View.OnClickListener {
EditText ename, erollnumber, emarks;
Button btnAdd, btnDelet, btnModify, btnView, btnViewall, btnShowinfo;
SQLiteDatabase db;
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
// Initializing controls
ename = (EditText) findViewById(R.id.id_name);
erollnumber = (EditText) findViewById(R.id.id_rollno);
emarks = (EditText) findViewById(R.id.id_marks);
btnAdd = (Button) findViewById(R.id.id_add_button);
btnDelet = (Button) findViewById(R.id.id_delet_button);
btnModify = (Button) findViewById(R.id.id_modifi_button);
btnView = (Button) findViewById(R.id.id_view_button);
btnViewall = (Button) findViewById(R.id.id_viewallbutton);
btnShowinfo = (Button) findViewById(R.id.id_show_button);
// Registering event handlers
btnAdd.setOnClickListener(this);
btnModify.setOnClickListener(this);
btnDelet.setOnClickListener(this);
btnView.setOnClickListener(this);
btnViewall.setOnClickListener(this);
btnShowinfo.setOnClickListener(this);
db = openOrCreateDatabase("StudentDB", Context.MODE_PRIVATE, null);
db.execSQL("CREATE TABLE IF NOT EXISTS student(rollno VARCHAR,name VARCHAR,marks VARCHAR);");
}
@Override
public void onClick(View v) {
// Adding a record
if (v == btnAdd) {// Checking empty fields
if (ename.getText().toString().trim().length() == 0 ||
erollnumber.getText().toString().trim().length() == 0 ||
emarks.getText().toString().trim().length() == 0) {
showMessage("ERROR", "PLZ PROVIDE ALL THE RECORD");
return;
}
// Inserting record
db.execSQL("INSERT INTO student VALUES('" + ename.getText() + "','" + erollnumber.getText() + "','" + emarks.getText() + "')");
showMessage("SUCESSFULLY", "RECORD ADDED SUCESSFULLY");
clearText();
}
if (v==btnDelet)
{
//check for emptu record
if (erollnumber.getText().toString().trim().length()==0)
{
showMessage("ERROR","PLZ ENTER ROLL NO" );
return;
}
// Searching roll number
Cursor c=db.rawQuery("SELECT * FROM student WHERE rollno='"+erollnumber.getText()+"'", null);
if(c.moveToFirst())
{
// Deleting record if found
db.execSQL("DELETE FROM student WHERE rollno='"+erollnumber.getText()+"'");
showMessage("Success", "Record Deleted");
}
else
{
showMessage("Error", "Invalid Rollno");
}
clearText();
}
// Modifying a record
if(v==btnModify)
{
// Checking empty roll number
if(erollnumber.getText().toString().trim().length()==0)
{
showMessage("Error", "Please enter Rollno");
return;
}
// Searching roll number
Cursor c=db.rawQuery("SELECT * FROM student WHERE rollno='"+erollnumber.getText()+"'", null);
if(c.moveToFirst())
{
// Modifying record if found
db.execSQL("UPDATE student SET name='"+ename.getText()+"',marks='"+erollnumber.getText()+
"' WHERE rollno='"+erollnumber.getText()+"'");
showMessage("Success", "Record Modified");
}
else
{
showMessage("Error", "Invalid Rollno");
}
clearText();
}
// Viewing a record
if(v==btnView)
{
// Checking empty roll number
if(erollnumber.getText().toString().trim().length()==0)
{
showMessage("Error", "Please enter Rollno");
return;
}
// Searching roll number
Cursor c=db.rawQuery("SELECT * FROM student WHERE rollno='"+erollnumber.getText()+"'", null);
if(c.moveToFirst())
{
// Displaying record if found
ename.setText(c.getString(1));
emarks.setText(c.getString(2));
}
else
{
showMessage("Error", "Invalid Rollno");
clearText();
}
}
// Viewing all records
if(v==btnViewall)
{
// Retrieving all records
Cursor c=db.rawQuery("SELECT * FROM student", null);
// Checking if no records found
if(c.getCount()==0)
{
showMessage("Error", "No records found");
return;
}
// Appending records to a string buffer
StringBuffer buffer=new StringBuffer();
while(c.moveToNext())
{
buffer.append("Rollno: "+c.getString(0)+"\n");
buffer.append("Name: "+c.getString(1)+"\n");
buffer.append("Marks: "+c.getString(2)+"\n\n");
}
// Displaying all records
showMessage("Student Details", buffer.toString());
}
// Displaying info
if(v==btnShowinfo)
{
showMessage("THIS LEARNING APP CREATED FOR LEARNING PURPOSE ONLY", "WRITTEN AGAIN FOR MY SELF HELP https://www.codeproject.com/Articles/783073/A-Simple-Android-SQLite-Example");
}
}
private void showMessage(String title, String message) {
Builder builder = new Builder(this);
builder.setCancelable(true);
builder.setTitle(title);
builder.setMessage(message);
builder.show();
}
public void clearText() {
ename.setText("");
erollnumber.setText("");
emarks.setText("");
erollnumber.requestFocus();
}
}
xml file
<?xml version="1.0" encoding="utf-8"?>
<android.support.constraint.ConstraintLayout xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:app="http://schemas.android.com/apk/res-auto"
xmlns:tools="http://schemas.android.com/tools"
android:layout_width="match_parent"
android:layout_height="match_parent"
tools:context="com.example.viraj.studentmanagementsystem.MainActivity"
android:background="#2196F3"
android:orientation="vertical"
>
<LinearLayout
android:layout_width="368dp"
android:layout_height="495dp"
android:orientation="vertical"
android:layout_marginLeft="8dp"
app:layout_constraintLeft_toLeftOf="parent"
app:layout_constraintTop_toTopOf="parent"
android:layout_marginTop="8dp"
android:layout_marginRight="8dp"
app:layout_constraintRight_toRightOf="parent"
app:layout_constraintBottom_toBottomOf="parent"
android:layout_marginBottom="8dp">
<EditText
android:id="@+id/id_rollno"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:layout_marginTop="10dp"
android:ems="10"
android:hint="ENTER ROLL NO"
android:inputType="textPersonName" />
<EditText
android:id="@+id/id_name"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:layout_marginTop="10dp"
android:ems="10"
android:hint="ENTER NAME" />
<EditText
android:id="@+id/id_marks"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:layout_marginTop="10dp"
android:ems="10"
android:hint="ENTER MARKS"
android:inputType="textPersonName" />
<Button
android:id="@+id/id_add_button"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:layout_marginTop="10dp"
android:background="#CDDC39"
android:text="ADD" />
<Button
android:id="@+id/id_delet_button"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:layout_marginTop="10dp"
android:background="#CDDC39"
android:text="DELETE" />
<Button
android:id="@+id/id_modifi_button"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:layout_marginTop="10dp"
android:background="#CDDC39"
android:text="MODIFY" />
<Button
android:id="@+id/id_view_button"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:layout_marginTop="10dp"
android:background="#CDDC39"
android:text="VIEW" />
<Button
android:id="@+id/id_viewallbutton"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:layout_marginTop="10dp"
android:background="#CDDC39"
android:text="VIEW ALL" />
<Button
android:id="@+id/id_show_button"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:layout_marginTop="10dp"
android:background="#CDDC39"
android:text="SHOW" />
</LinearLayout>
</android.support.constraint.ConstraintLayout>
0 comments:
Post a Comment