简体   繁体   中英

SUM sqlite column values (android)

I need help with summing all the values in one of the columns( KEY_CONTENT6 ) in my database( MYDATABASE_TABLE ). I keep having force close issues, somebody tell me what I'm doing wrong!

Heres my code:

public Cursor sumAll(){

 float columntotal = 0;
 Cursor cursor1 = sqLiteDatabase.rawQuery(
     "SELECT SUM("+Float.valueOf(KEY_CONTENT6)+") FROM MYDATABASE_TABLE", null);
       if(cursor1.moveToFirst()) {
         columntotal = cursor1.getFloat(0);
     }
   cursor1.close();

 String  sumtotal = Float.toString((float)columntotal);           

  return cursor1;

 }

The column KEY_CONTENT6 is a string for various reasons which is why I used Float.valueOf() but I'm guessing that's where my problem is.

You're closing the cursor then returning it. You probably want to return the column total or sum total, not the cursor

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM