-1

My query is

Button.setOnClickListener(new View.OnClickListener() {
        @Override
        public void onClick(View v) {
            int cv;

            String getmont="",getyear="";

            getmont=year.getText().toString();
            getyear= (String) sid.getSelectedItem();
            SQLiteDatabase db;

            db = openOrCreateDatabase("monthapp", MODE_PRIVATE, null);
            curtop = db.rawQuery("SELECT SUM(currentpaid)FROM monthvaluesOrgin WHERE curmonth='" + getmont + "' AND curyear='" + getyear + "'", null);
            if (curtop.moveToNext() ) {

              cv=curtop.getInt(0);
                Toast.makeText(getApplicationContext(),cv,Toast.LENGTH_SHORT).show();


            }


        }
    });

How to print/ toast the sum value ?

It always showing EOFException

I have tried with in the try catch but not working.

OneCricketeer
  • 179,855
  • 19
  • 132
  • 245
sukesh
  • 95
  • 1
  • 1
  • 6

1 Answers1

0

Assume cursor is your cursor returned by the rawQuery() call. So, use that to get int sum value:

int sum;
if(cursor.moveToFirst()) sum = c.getInt(0); 
else sum = -1;
c.close();

And then you can use sum whatever you want.

Valentun
  • 1,661
  • 12
  • 23
  • No I need to get sum of all values of column in the table May you're right but I'm not understanding Thanks – sukesh Aug 06 '17 at 15:36
  • @sukesh but your sql code calculate sum for you. You just need to get it from the Cursor object – Valentun Aug 06 '17 at 15:38
  • I tried this Cursor c=db.rawQuery("SELECT SUM(PAID)AS TOTAL FROM tablename where id='"+Id+"'" ,null); While (cursor.moveTonext) {Int sum; sum=cursor.getInt(2); Toast.makeText(getApplicationcontext,sum,2000).Show(); But not working } – sukesh Aug 06 '17 at 15:48
  • @sukesh but you should call cursor.getInt(0), not getInt(2). Remember, result cursor you got from rawQuery() in your case is a single row with single column. – Valentun Aug 06 '17 at 15:51
  • I tried but But it Shows EofException And Error deserializing sampled events – sukesh Aug 07 '17 at 15:08
  • That also not working please see my code above – sukesh Aug 08 '17 at 10:07
  • @sukesh to show int in toast you need to use String.valueOf(cv) instead of just cv. – Valentun Aug 09 '17 at 01:24