1 Replies - 604 Views - Last Post: 17 August 2015 - 04:35 AM

#1 antara21   User is offline

  • New D.I.C Head

Reputation: 0
  • View blog
  • Posts: 1
  • Joined: 14-August 15

I am trying my hands on creating basic Android App using Sqlite db

Posted 17 August 2015 - 03:32 AM

Trying to create a basic Android App in Android Studio using Sqlite which would do simple Select,Insert,Update and Delete operations on various button clicks. But my application is getting forced stop whenever I click on the buttons.
Can anyone please take out sometime to see where exactly is teh error in the code and how do I fix it to run it properly?
Below is the code for MainActivity.java :

package com.example.antar.myapplication;

import android.os.Bundle;
import android.support.v7.app.ActionBarActivity;
import android.view.View;
import android.widget.Button;
import android.widget.EditText;
import android.widget.TextView;
import android.database.Cursor;
import android.database.sqlite.SQLiteDatabase;
import android.database.sqlite.SQLiteOpenHelper;
import android.provider.BaseColumns;
import android.app.Activity;


public class MainActivity extends ActionBarActivity implements View.onclickListener{
    TextView textView;
    private Button btnAdd,btnDelete,btnModify,btnView,btnViewAll,btnShowInfo;
    private EditText editRollno,editName,editMarks;
    private SQLiteDatabase db;

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

        // Initializing controls
        editRollno=(EditText)findViewById(R.id.etRollNum);
        editName=(EditText)findViewById(R.id.etName);
        editMarks=(EditText)findViewById(R.id.etMarks);
        btnAdd=(Button)findViewById(R.id.btnAdd);
        btnDelete=(Button)findViewById(R.id.btnDelete);
        btnModify=(Button)findViewById(R.id.btnModify);
        btnView=(Button)findViewById(R.id.btnView);
        btnViewAll=(Button)findViewById(R.id.btnViewAll);
        btnShowInfo=(Button)findViewById(R.id.btnShow);

        // Registering event handlers
        btnAdd.setonclickListener(this);
        btnDelete.setonclickListener(this);
        btnModify.setonclickListener(this);
        btnView.setonclickListener(this);
        btnViewAll.setonclickListener(this);
        btnShowInfo.setonclickListener(this);

        // Creating database and table
        db=openOrCreateDatabase("StudentDB", android.content.Context.MODE_PRIVATE, null);
        db.execSQL("CREATE TABLE IF NOT EXISTS student(rollno VARCHAR,name VARCHAR,marks VARCHAR);");
    }

 public void showGreetings(View view)
{
    String message = "Welcome..";
    textView.setText(message);
}

    @Override
    public void onclick(View view)
    {
        // Adding a record
        if(view==btnAdd)
        {
            // Checking empty fields
            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");
                String message = "Error! Please enter all values";
                textView.setText(message);
                return;
            }
            // Inserting record
            db.execSQL("INSERT INTO student VALUES('"+editRollno.getText()+"','"+editName.getText()+
                    "','"+editMarks.getText()+"');");
            //showMessage("Success", "Record added");
            String message = "Success! Record added";
            textView.setText(message);
            clearText();
        }
        // Deleting a record
        if(view==btnDelete)
        {
            // Checking empty roll number
            if(editRollno.getText().toString().trim().length()==0)
            {
                //showMessage("Error", "Please enter Rollno");
                String message = "Error! Please enter Rollno";
                textView.setText(message);
                return;
            }
            // Searching roll number
            Cursor c=db.rawQuery("SELECT * FROM student WHERE rollno='"+editRollno.getText()+"'", null);
            if(c.moveToFirst())
            {
                // Deleting record if found
                db.execSQL("DELETE FROM student WHERE rollno='"+editRollno.getText()+"'");
                //showMessage("Success", "Record Deleted");
                String message = "Success! Record Deleted";
                textView.setText(message);
            }
            else
            {
                //showMessage("Error", "Invalid Rollno");
                String message = "Error! Invalid Rollno";
                textView.setText(message);
            }
            clearText();
        }
        // Modifying a record
        if(view==btnModify)
        {
            // Checking empty roll number
            if(editRollno.getText().toString().trim().length()==0)
            {
                //showMessage("Error", "Please enter Rollno");
                String message = "Error! Please enter Rollno";
                textView.setText(message);
                return;
            }
            // Searching roll number
            Cursor c=db.rawQuery("SELECT * FROM student WHERE rollno='"+editRollno.getText()+"'", null);
            if(c.moveToFirst())
            {
                // Modifying record if found
                db.execSQL("UPDATE student SET name='"+editName.getText()+"',marks='"+editMarks.getText()+
                        "' WHERE rollno='"+editRollno.getText()+"'");
                //showMessage("Success", "Record Modified");
                String message = "Success! Record Modified";
                textView.setText(message);
            }
            else
            {
                //showMessage("Error", "Invalid Rollno");
                String message = "Error! Invalid Rollno";
                textView.setText(message);
            }
            clearText();
        }
        // Viewing a record
        if(view==btnView)
        {
            // Checking empty roll number
            if(editRollno.getText().toString().trim().length()==0)
            {
                //showMessage("Error", "Please enter Rollno");
                String message = "Error! Please enter Rollno";
                textView.setText(message);
                return;
            }
            // Searching roll number
            Cursor c=db.rawQuery("SELECT * FROM student WHERE rollno='"+editRollno.getText()+"'", null);
            if(c.moveToFirst())
            {
                // Displaying record if found
                editName.setText(c.getString(1));
                editMarks.setText(c.getString(2));
            }
            else
            {
                //showMessage("Error", "Invalid Rollno");
                String message = "Error! Invalid Rollno";
                textView.setText(message);
                clearText();
            }
        }
        // Viewing all records
        if(view==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");
                String message = "Error! No Records found";
                textView.setText(message);
                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());
            String message = "Student Details\", buffer.toString()";
            textView.setText(message);
        }
        // Displaying info
        if(view==btnShowInfo)
        {
            //showMessage("Student Management Application", "Developed By Azim");
            String message = "Student Management Application";
            textView.setText(message);
        }
    }


    public void clearText()
    {
        editRollno.setText("");
        editName.setText("");
        editMarks.setText("");
        editRollno.requestFocus();
    }
}



---------------------------------------------------------

Below is the code for the layout file (activity_main.xml) :


<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"
    xmlns:tools="http://schemas.android.com/tools" android:layout_width="match_parent"
    android:layout_height="match_parent" android:paddingLeft="@dimen/activity_horizontal_margin"
    android:paddingRight="@dimen/activity_horizontal_margin"
    android:paddingTop="@dimen/activity_vertical_margin"
    android:paddingBottom="@dimen/activity_vertical_margin" tools:context=".MainActivity">

    <TextView
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:text="Greetings"
        android:id="@+id/greetings_text_view"
        />

    <TextView
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:textAppearance="?android:attr/textAppearanceLarge"
        android:text="Student Information"
        android:id="@+id/textView"
        android:layout_centerHorizontal="true"
        android:textColor="#008888"
        android:layout_below="@+id/greetings_text_view" />

    <TextView
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:textAppearance="?android:attr/textAppearanceMedium"
        android:text="Roll No."
        android:id="@+id/textView2"
        android:layout_alignParentLeft="true"
        android:layout_alignParentStart="true"
        android:layout_below="@+id/textView" />

    <EditText
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:inputType="number"
        android:ems="10"
        android:id="@+id/etRollNum"
        android:layout_below="@+id/textView2"
        android:layout_alignParentLeft="true"
        android:layout_alignParentStart="true" />

    <TextView
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:textAppearance="?android:attr/textAppearanceMedium"
        android:text="Name"
        android:id="@+id/textView3"
        android:layout_alignParentLeft="true"
        android:layout_alignParentStart="true"
        android:layout_below="@+id/etRollNum" />

    <TextView
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:textAppearance="?android:attr/textAppearanceLarge"
        android:id="@+id/tvResult"
        android:layout_alignParentBottom="true"
        android:layout_toLeftOf="@+id/textView"
        android:layout_toStartOf="@+id/textView"
        android:layout_marginBottom="108dp"
        android:textColor="#00FFFF" />

    <EditText
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:inputType="textPersonName"
        android:ems="10"
        android:id="@+id/etName"
        android:layout_below="@+id/textView3"
        android:layout_alignParentLeft="true"
        android:layout_alignParentStart="true" />

    <TextView
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:textAppearance="?android:attr/textAppearanceMedium"
        android:text="Marks"
        android:id="@+id/textView4"
        android:layout_below="@+id/etName"
        android:layout_alignParentLeft="true"
        android:layout_alignParentStart="true" />

    <EditText
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:inputType="number"
        android:ems="10"
        android:id="@+id/etMarks"
        android:layout_below="@+id/textView4"
        android:layout_alignParentLeft="true"
        android:layout_alignParentStart="true" />

    <Button
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:text="Add"
        android:id="@+id/btnAdd"
        android:layout_below="@+id/etMarks"
        android:layout_alignParentLeft="true"
        android:layout_alignParentStart="true" />

    <Button
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:text="Delete"
        android:id="@+id/btnDelete"
        android:layout_below="@+id/etMarks"
        android:layout_alignRight="@+id/etMarks"
        android:layout_alignEnd="@+id/etMarks" />

    <Button
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:text="Modify"
        android:id="@+id/btnModify"
        android:layout_alignBottom="@+id/tvResult"
        android:layout_alignParentLeft="true"
        android:layout_alignParentStart="true" />

    <Button
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:text="View"
        android:id="@+id/btnView"
        android:layout_alignTop="@+id/btnModify"
        android:layout_alignLeft="@+id/btnDelete"
        android:layout_alignStart="@+id/btnDelete" />

    <Button
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:text="View All"
        android:id="@+id/btnViewAll"
        android:layout_alignParentBottom="true"
        android:layout_alignParentLeft="true"
        android:layout_alignParentStart="true" />

    <Button
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:text="Show"
        android:id="@+id/btnShow"
        android:layout_alignParentBottom="true"
        android:layout_alignLeft="@+id/btnView"
        android:layout_alignStart="@+id/btnView" />
</RelativeLayout>




--------------------------------------------------------------

Thanks in advance!

This post has been edited by modi123_1: 17 August 2015 - 07:24 AM
Reason for edit:: please use the 'code' tag button in the editor


Is This A Good Question/Topic? 0
  • +

Replies To: I am trying my hands on creating basic Android App using Sqlite db

#2 ndc85430   User is offline

  • I think you'll find it's "Dr"
  • member icon

Reputation: 976
  • View blog
  • Posts: 3,849
  • Joined: 13-June 14

Re: I am trying my hands on creating basic Android App using Sqlite db

Posted 17 August 2015 - 04:35 AM

I can't help with Android, but a few things:

1. Please post code within code tags here and make sure it is indented in a sensible way. It's difficult to read, otherwise.

2. You should always be using prepared statements instead of using string concatenation to put your values in your SQL statements. See also Exploits of a Mom.

This post has been edited by ndc85430: 17 August 2015 - 04:46 AM

Was This Post Helpful? 0
  • +
  • -

Page 1 of 1