Welcome to OGeek Q&A Community for programmer and developer-Open, Learning and Share
Welcome To Ask or Share your Answers For Others

Categories

0 votes
628 views
in Technique[技术] by (71.8m points)

sqlite - Android get sum of database column

I need help with summing all the values in one of the columns(amount) in my database. i can a get a particular value. .but i need sum of a specific column, somebody tell me what I'm doing wrong

This is my code

    Button button3 = (Button) findViewById(R.id.button3);
    button3.setOnClickListener(new OnClickListener() {          
       public void onClick(View v) {                
          DatabaseAdapter databaseAdapter = new DatabaseAdapter(getApplicationContext());
          databaseAdapter.open();
          ArrayList<String> records = databaseAdapter.fetchAllRecords();
          if (records.size() > 0) {
              et.setText(records.get(0));
          }
          databaseAdapter.close();
      }
   });

    //Create our database by opening it and closing it
    DatabaseAdapter databaseAdapter = new DatabaseAdapter(getApplicationContext());
    databaseAdapter.open();
    databaseAdapter.close();
} 

private Object append(CharSequence text) {
    // TODO Auto-generated method stub
    return null;
}

/** Create a new dialog for date picker */
@Override
protected Dialog onCreateDialog(int id) {
    switch (id) {
       case DATE_DIALOG_ID:
         return new DatePickerDialog(this, pDateSetListener, pYear, pMonth, pDay);
    }
    return null;
}
}

This is the database part DatabaseAdapter.java

package com.example.androidtablayout;

import java.util.ArrayList;
import android.content.ContentValues;
import android.content.Context;
import android.database.Cursor;
import android.database.SQLException;
import android.database.sqlite.SQLiteDatabase;
import android.text.Editable;

public class DatabaseAdapter {

    private Context context;
    private SQLiteDatabase database;
    private DatabaseOpenHelper dbHelper;

    public DatabaseAdapter(Context context) {
       this.context = context;
    }

    public DatabaseAdapter open() throws SQLException {
       dbHelper = new DatabaseOpenHelper(context);
       database = dbHelper.getWritableDatabase();
       return this;
    }

    public void close() {
       dbHelper.close();
    }


    public long createRecord(String text,int j,String text1,String text2) {
        ContentValues contentValue = new ContentValues();       
        contentValue.put("date", text);     
        contentValue.put("amount", j);  
        contentValue.put("des", text1);
        contentValue.put("category", text2);
        return database.insert("Extable", null, contentValue);
    }

    public boolean updateRecord(long rowId,String text,int j,String text1,String text2) {
        ContentValues contentValue = new ContentValues();
        contentValue.put("date", text);     
        contentValue.put("amount", j);  
        contentValue.put("des", text1);
        contentValue.put("category", text2);
        return database.update("Extable", contentValue, "_id =" + rowId, null) > 0;
    }

    public boolean deleteRecord(long rowId) {
        return database.delete("Extable", "_id =" + rowId, null) > 0;
    }

    public ArrayList<String> fetchAllRecords() {
        Cursor cursor = database.query("Extable", new String[] { "_id", "date", "amount", "des", "category"},
                null, null, null, null, null);      
        ArrayList<String> records = new ArrayList<String>();        
        cursor.moveToFirst();
        for (int i = 0; i < cursor.getCount(); i++) {           
            records.add(cursor.getString(1));       
            cursor.moveToNext();
        }
        cursor.close();
        return records;
    }

    public String fetchRecord(long rowId) throws SQLException {
        Cursor mCursor = database.query(true, "Extable", new String[] { "_id",
                        "date","amount", "des","category" }, "_id ="+ rowId, null, null, null, null, null);
        if (mCursor != null) {
            mCursor.moveToFirst();
            return (mCursor.getString(1));
        }
        return null;
    }

    public Cursor rawQuery(String string, Object object) {
        // TODO Auto-generated method stub
        return null;
    }

Pls anyone help to me. Thank u

See Question&Answers more detail:os

与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
Welcome To Ask or Share your Answers For Others

1 Reply

0 votes
by (71.8m points)

Do so:

Cursor cur = db.rawQuery("SELECT SUM(myColumn) FROM myTable", null);
if(cur.moveToFirst())
{
    return cur.getInt(0);
}

与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
OGeek|极客中国-欢迎来到极客的世界,一个免费开放的程序员编程交流平台!开放,进步,分享!让技术改变生活,让极客改变未来! Welcome to OGeek Q&A Community for programmer and developer-Open, Learning and Share
Click Here to Ask a Question

...