【发布时间】:2014-09-24 02:37:23
【问题描述】:
我正在尝试使用以下方法来计算我的数据库中某一列中所有值的总和。
/**
* Method that gives the the total of all
* Average Meditation levels in the DB
* @return
*/
public Cursor getTotalOfAllMedLevels(){
SQLiteDatabase db = this.getWritableDatabase();
String query = "SELECT SUM(avgmeditation) FROM " + TABLE_SCORE;
Cursor c = db.rawQuery(query, null);
return c;
}
然后我尝试在不同的 Activity 中显示 TextView (avgMed) 中方法的返回值,如下所示:
public void displayAverageOfAllMedValues() {
Cursor c = db.getTotalOfAllMedLevels();
avgMed.setText("" + c);
}
但是在TextView中得到如下输出:
android.database.sqlite.SQLiteCursor@529db063
有没有办法将此输出转换为 int 值,或者如何更改我的代码以提供我想要的 Int 输出。
编辑:当前数据库的创建和结构:
public class DatabaseHelper extends SQLiteOpenHelper {
// Database Version
private static final int DATABASE_VERSION = 10;
// Database Name
private final static String DATABASE_NAME = "MeditationDatabase";
// Contacts table name
private static final String TABLE_SCORE = "scores";
// Contacts Table Columns names
private static final String COL_SESSION = "sessionid";
private static final String COL_GAMETITLE = "game";
private static final String COL_NAME = "name";
private static final String COL_MED = "avgmeditation";
private static final String COL_MAX = "maxmeditation";
private static final String COL_AVGATT = "avgattention";
private static final String COL_MAXATT = "maxattention";
private static final String COL_SCORE = "score";
private static final String COL_DATE = "date";
/**
* Constructor
*
* @param context
*/
public DatabaseHelper(Context context) {
super(context, DATABASE_NAME, null, DATABASE_VERSION);
}
/**
* Method that creates the database
*/
@Override
public void onCreate(SQLiteDatabase db) {
String CREATE_TABLE_SCORE = "CREATE TABLE " + TABLE_SCORE + "(" + COL_SESSION
+ " STRING PRIMARY KEY, " + COL_GAMETITLE + " STRING, " + COL_NAME + " STRING, " + COL_MED + " INTEGER, "
+ COL_MAX + " INTEGER, " + COL_AVGATT + " INTEGER, " + COL_MAXATT + " INTEGER, " + COL_SCORE + " INTEGER, " + COL_DATE + " STRING " + ")";
db.execSQL(CREATE_TABLE_SCORE);
}
【问题讨论】: