Skip to main content

Insert, Delete and view the data from the database -Android

MyApp.java

package dharan.dharani.com.myapplication;

/**

 * Created by vdharanidharan on 04-Jul-17.

 */

import android.app.Activity;
import android.app.AlertDialog.Builder;
import android.content.Context;
import android.database.Cursor;
import android.database.sqlite.SQLiteDatabase;
import android.os.Bundle;
import android.view.View;
import android.view.View.OnClickListener;
import android.widget.Button;
import android.widget.EditText;
import static android.R.id.message;
import static dharan.dharani.com.myapplication.R.attr.title;
import static dharan.dharani.com.myapplication.R.string.view;

public class MyApp extends Activity implements OnClickListener
{
    EditText editRollno,editName,editMarks;
    Button btnAdd,btnDelete,btnModify,btnView,btnViewAll,btnShowInfo;
    SQLiteDatabase db;
    /** Called when the activity is first created. */
    @Override

    public void onCreate(Bundle savedInstanceState)

    {

        super.onCreate(savedInstanceState);
        setContentView(R.layout.main);
        editRollno=(EditText)findViewById(R.id.editRollno);
        editName=(EditText)findViewById(R.id.editName);
        editMarks=(EditText)findViewById(R.id.editMarks);
        btnAdd=(Button)findViewById(R.id.btnAdd);
        btnDelete=(Button)findViewById(R.id.btnDelete);
        btnView=(Button)findViewById(R.id.btnView);
        btnAdd.setOnClickListener(this);
        btnDelete.setOnClickListener(this);
        btnView.setOnClickListener(this);
        db=openOrCreateDatabase("StudentDB", Context.MODE_PRIVATE, null);
        db.execSQL("CREATE TABLE IF NOT EXISTS student(rollno VARCHAR,name VARCHAR,marks VARCHAR);");

    }

    public void onClick(View view) {
        if (view == btnAdd) {
            if (editRollno.getText().toString().trim().length() == 0 ||
                    editName.getText().toString().trim().length() == 0 ||
                    editMarks.getText().toString().trim().length() == 0) {
                showMessage("Error", "Please enter all values");
                return;
            }
            db.execSQL("INSERT INTO student VALUES('" + editRollno.getText() + "','" + editName.getText() +
                    "','" + editMarks.getText() + "');");
            showMessage("Success", "Record added To sql lite database");
            clearText();
        }
        if(view == btnDelete)
        {
            if(editRollno.getText().toString().trim().length()==0)
            {
                showMessage("Error", "Please enter Rollno");
                return;
            }
            Cursor c=db.rawQuery("SELECT * FROM student WHERE rollno='"+editRollno.getText()+"'", null);
            if(c.moveToFirst())
            {
                db.execSQL("DELETE FROM student WHERE rollno='"+editRollno.getText()+"'");
                showMessage("Success", "Record Deleted");
            }
            else
            {
                showMessage("Error", "Invalid Rollno");
            }
            clearText();
        }
        if(view==btnView)
        {
            if(editRollno.getText().toString().trim().length()==0)
            {
                showMessage("Error", "Please enter Rollno");
                return;
            }
            Cursor c=db.rawQuery("SELECT * FROM student WHERE rollno='"+editRollno.getText()+"'", null);
            if(c.moveToFirst())
            {
                editName.setText(c.getString(1));
                editMarks.setText(c.getString(2));
            }
            else
            {
                showMessage("Error", "Invalid Rollno");
                clearText();

            }

        }

    }
        public 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()
        {
            editRollno.setText("");
            editName.setText("");
            editMarks.setText("");
            editRollno.requestFocus();

        }
}

Main.xml

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

<AbsoluteLayout xmlns:android="http://schemas.android.com/apk/res/android"

    android:id="@+id/myLayout"
    android:stretchColumns="0"
    android:layout_width="fill_parent"
    android:layout_height="fill_parent">

    <TextView android:text="@string/title"

        android:layout_x="110dp"
        android:layout_y="10dp"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:textSize="24sp" />

    <TextView android:text="@string/roll_no"

        android:layout_x="30dp"
        android:layout_y="50dp"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:textSize="18sp" />

    <TextView android:text="@string/name"

        android:layout_x="30dp"
        android:layout_y="100dp"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:textSize="18sp" />

    <TextView android:text="@string/marks"

        android:layout_x="30dp"
        android:layout_y="150dp"
        android:layout_width="134dp"
        android:layout_height="50dp"
        android:textSize="18sp" />
 
    <Button   android:id="@+id/btnView"

        android:text="@string/view"
        android:layout_x="115dp"
        android:layout_y="366dp"
        android:layout_width="138dp"
        android:layout_height="73dp"/>

    <Button      android:id="@+id/btnAdd"

        android:text="@string/add"
        android:layout_x="41dp"
        android:layout_y="268dp"
        android:layout_width="124dp"
        android:layout_height="70dp"/>

    <Button      android:id="@+id/btnDelete"

        android:text="@string/delete"
        android:layout_x="234dp"
        android:layout_y="264dp"
        android:layout_width="120dp"
        android:layout_height="68dp"/>

    <EditText android:id="@+id/editRollno"

        android:inputType="number"
        android:layout_x="195dp"
        android:layout_y="46dp"
        android:layout_width="150dp"
        android:layout_height="40dp"/>

    <EditText android:id="@+id/editName"

        android:inputType="text"
        android:layout_x="187dp"
        android:layout_y="90dp"
        android:layout_width="150dp"
        android:layout_height="wrap_content"/>

    <EditText android:id="@+id/editMarks"

        android:inputType="number"
        android:layout_x="191dp"
        android:layout_y="144dp"
        android:layout_width="150dp"
        android:layout_height="40dp"/>

</AbsoluteLayout>


OUTPUT:




Thanks and regards,
Tech bird

Comments

Popular posts from this blog

BIG DATA ANALYTICS

BIG DATA ANALYTICS Have you ever hit upon how Amazon and Flip kart could possible verdict what we want; how the Google auto completes our search; how the YouTube looks into videos we want to watch? When we open YouTube, we will be at sixes and sevens, when we find ads related to what we have searched earlier in the past days. This is where we find ourselves in the era of big data analytics. More than 3 trillion bytes of information are being generated everyday through our smart phones, tablets, GPS devices, etc.  Have we thought about what can be done with all these information? This is where the data analytics comes into play. Big data analytics is just the study of future build up to store data in order to extract the behaviour patterns. The entire social networking website gathers our data which are related to our interest which is usually done by using our past search or any other social information. Data analytics will lead to a walkover in near future....

CLOUD COMPUTING SERVICES

Services provided by Cloud Computing   1. Software as a service :  It is process by which the software will be provided by the cloud server's. Instead of using the software in our local machine we can directly use the cloud services directly to get our work done.   Example : Google Slides, Google Docs, Google Sheets, Zoho Writer,....etc. 2. Platform as a service : It is process by which you can directly use platform like windows , linux , Mac, ....Which ever you need to done your work. Example : There are certain software which might be run only certain platform. Assume I have windows operating system but i am doing research in BIG DATA. There Linux Operation system might be handy for my research work. Where it is hard for me to put Operating System again and again to my local machine and taking backup data. So Cloud Computing Services Makes our work easier whatever platform we need it will provide u...

Hidden things About Amazon SageMaker Studio

Did you know about Amazon SageMaker Studio❓ 🤔 Like you, I initially believed that this service was only for data-related tasks and that regular engineers/developers weren't supposed to use it. ✒️ However, after using it for a while, I would suggest that it can help you with more than just data related tasks. In fact, an organization can use SageMaker Studio to bring their entire SDLC 💪. 😬 Because of its data'ish ness like gimick we (normal non-data developers) always felt, "Oh, SageMaker, it's expensive 😱 so no, no don't go that side 🤐." 😷 As a result, we shrank and missed the hidden gem 💎 and its possibilities, as well as the opportunity to utilize such a fantastic and powerful tool 🔥. ✒️ Let me give you some glimpse with a preview of what SageMaker Studio is capable of. ✒️ SageMaker is big service, but in this post am limiting my context towards SageMaker Studio only. ✒️ And mostly, this write-up is for developers who enjoy writin...