samedi 2 mai 2015

unable to retrieve the data from sqlite

Please help me.The application is working perfectly for inserting the data into sqlite database. But while getting the data it is not working. It is displaying unfortunately the app is stopped. How to solve this problem.

 MianActivity.java      

    package com.example.nagaraju.sqlitedemo;

    import android.app.AlertDialog;
    import android.content.Intent;
    import android.database.Cursor;
    import android.support.v7.app.ActionBarActivity;
    import android.os.Bundle;
    import android.view.Menu;
    import android.view.MenuItem;
    import android.view.View;
    import android.widget.Button;
    import android.widget.EditText;
    import android.widget.Toast;


    public class MainActivity extends ActionBarActivity {

        Database ob;// creating an object for the database class
        EditText rno,name;
        Button save;
        Button get;


        @Override
        protected void onCreate(Bundle savedInstanceState) {
            super.onCreate(savedInstanceState);
            setContentView(R.layout.activity_main);
            ob =new  Database(this);

           rno =(EditText)findViewById(R.id.editText);
           name =(EditText)findViewById(R.id.editText2);
            save=(Button)findViewById(R.id.save_button);
            get= (Button)findViewById(R.id.get_button);
          AddData();
            viewData();


        }
        public void AddData() {

            save.setOnClickListener(new View.OnClickListener() {
                                        @Override
                                        public void onClick(View v) {
                                            boolean isInserted =ob.insertData(rno.getText().toString(),name.getText().toString());

                                            if (isInserted==true)
                                                Toast.makeText(MainActivity.this, "Data Inserted", Toast.LENGTH_LONG).show();
                                            else
                                                Toast.makeText(MainActivity.this, "sorry", Toast.LENGTH_LONG).show();
    // Retrieve the text entered from the EditText

                                        }
                                    }
            );

        }


        public void viewData()
        {
            get.setOnClickListener(new View.OnClickListener() {
                                       @Override
                                       public void onClick(View v) {
                                           Cursor result = ob.getData();
                                           if (result.getCount()==0){
                                               //show message
                                               //Toast.makeText(MainActivity.this, "hai", Toast.LENGTH_LONG).show();
                                               showData("error","no data");

                                               return;
                                           }
                                           StringBuffer buffer= new StringBuffer();

                                           while(result.moveToNext()){
                                               buffer.append("RNo:"+result.getString(0)+"\n");
                                               buffer.append("Name:"+result.getString(1)+"\n\n");
                                           }

                                           showData("Data","buffer.toString()");
                                       }
                                   }
            );


        }

        public void showData(String title,String msg){

            AlertDialog.Builder builder= new  AlertDialog.Builder(this);
            builder.setCancelable(true);
            builder.setTitle(title);
            builder.setMessage(msg);
            builder.show();
        }


        @Override
        public boolean onCreateOptionsMenu(Menu menu) {
            // Inflate the menu; this adds items to the action bar if it is present.
            getMenuInflater().inflate(R.menu.menu_main, menu);
            return true;
        }

        @Override
        public boolean onOptionsItemSelected(MenuItem item) {
            // Handle action bar item clicks here. The action bar will
            // automatically handle clicks on the Home/Up button, so long
            // as you specify a parent activity in AndroidManifest.xml.
            int id = item.getItemId();

            //noinspection SimplifiableIfStatement
            if (id == R.id.action_settings) {
                return true;
            }

            return super.onOptionsItemSelected(item);
        }
    }
    Database.java
    public class Database extends SQLiteOpenHelper {

        public static final String database_name="Nagu.db";
        public static final String Table_name="student";
        public static final String regNo ="regNo";
        public static final String name="name";
        public Database(Context context) {
            super(context, database_name, null, 1);


        }

        @Override
        public void onCreate(SQLiteDatabase db) {

           //db.execSQL("create table Table_name " + "(regNo integer primary key, name text)" );

            db.execSQL("CREATE TABLE IF NOT EXISTS student(regNo integer,name VARCHAR)");

            /* String CREATE_TABLE = "CREATE TABLE " +
                    student2 + "("
                    + regNo + " INTEGER PRIMARY KEY," + name
                    + " TEXT" + ")"; */



            // create books table
            //db.execSQL(CREATE_TABLE);
        }


        @Override
        public void onUpgrade(SQLiteDatabase db, int oldVersion, int newVersion) {

            db.execSQL("DROP TABLE IF EXISTS student");
            onCreate(db);
        }


       public boolean insertData(String rno, String stuname)    {

    SQLiteDatabase db=this.getWritableDatabase();
            ContentValues values = new ContentValues();
            values.put(regNo, rno);
            values.put(name, stuname);
           long result= db.insert(Table_name,null,values);
            if (result==-1)
                return false;
            else
                return true;


        }

        public Cursor getData(){

            SQLiteDatabase db=this.getWritableDatabase();

            Cursor result= db.rawQuery("select * from" + Table_name,null);

            return result;

        }
    }
    activity_main.xml

    <RelativeLayout xmlns:android="http://ift.tt/nIICcg"
        xmlns:tools="http://ift.tt/LrGmb4" 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"
        android:id="@+id/main">


        <TextView
            android:layout_width="wrap_content"
            android:layout_height="wrap_content"
            android:textAppearance="?android:attr/textAppearanceLarge"
            android:text="RegdNo"
            android:id="@+id/regdno_textView"
            android:layout_alignParentTop="true"
            android:layout_alignParentLeft="true"
            android:layout_alignParentStart="true" />

        <TextView
            android:layout_width="wrap_content"
            android:layout_height="wrap_content"
            android:textAppearance="?android:attr/textAppearanceLarge"
            android:text="StudentName"
            android:id="@+id/studentname_textView2"
            android:layout_below="@+id/regdno_textView"
            android:layout_alignParentLeft="true"
            android:layout_alignParentStart="true"
            android:layout_marginTop="77dp" />

        <EditText
            android:layout_width="wrap_content"
            android:layout_height="wrap_content"
            android:id="@+id/editText2"
            android:layout_alignTop="@+id/studentname_textView2"
            android:layout_toRightOf="@+id/studentname_textView2"
            android:layout_alignRight="@+id/editText"
            android:layout_alignEnd="@+id/editText" />

        <Button
            android:layout_width="wrap_content"
            android:layout_height="wrap_content"
            android:text="save"
            android:id="@+id/save_button"
            android:layout_centerVertical="true"
            android:layout_alignParentLeft="true"
            android:layout_alignParentStart="true"
            android:nestedScrollingEnabled="false" />

        <EditText
            android:layout_width="wrap_content"
            android:layout_height="wrap_content"
            android:inputType="number"
            android:ems="10"
            android:id="@+id/editText"
            android:layout_alignTop="@+id/regdno_textView"
            android:layout_toRightOf="@+id/studentname_textView2"
            android:layout_toEndOf="@+id/studentname_textView2" />

        <Button
            android:layout_width="wrap_content"
            android:layout_height="wrap_content"
            android:text="GET"
            android:id="@+id/get_button"
            android:layout_centerVertical="true"
            android:layout_alignParentRight="true"
            android:layout_alignParentEnd="true" />
    </RelativeLayout>

Aucun commentaire:

Enregistrer un commentaire