我需要帮助汇总数据库中某列(金额)中的所有值。我可以得到一个特定的价值。但是我需要一个特定列的总和,有人告诉我我做错了什么
这是我的代码
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;
}
}
这是数据库部分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;
}
请任何人帮助我。 谢谢你
答案 0 :(得分:29)
这样做:
Cursor cur = db.rawQuery("SELECT SUM(myColumn) FROM myTable", null);
if(cur.moveToFirst())
{
return cur.getInt(0);
}