从SQLite数据库的列中获取总和和平均值

时间:2017-02-26 11:04:40

标签: android sqlite android-studio android-sqlite

Goodmorning everyone!

我试图从android中的sqlite数据库的列中获得总和和平均值。 搜索我找到了很多方法,我尝试了它们没有成功。

要获取已使用的列中的值数:

cursor.getCount() // with the cursor in the right column

它有效。我的问题是如何获得总和。 这是I"构建的方法":

public void setValues()
{
    SQLiteDatabase db = mDbHelper.getReadableDatabase();

    String[] projection = {
            _ID,
            DiabeticContract.DiabeticEntry.COLUMN_GLYCAEMIA_VALUES,
    };


    Cursor curs = db.query(
            DiabeticContract.DiabeticEntry.TABLE_NAME,   
            projection,            
            null,                  
            null,                  
            null,              
            null,                  
            null );                  

    curs.moveToLast();

    double lastGlycValue = curs.getDouble( curs.getColumnIndex("Glycaemia") );
    double percent = (lastGlycValue + 46.7) / 28.7;
    double mmol = ((percent  - 2.15) * 10.929);

    LastGlyc.setText(String.format("%.2f" ,lastGlycValue));
    Percent.setText(String.format("%.2f" ,percent));
    mMol.setText(String.format("%.2f" ,mmol));
}

没有cursor.getCount()可以让它更清晰。

感谢您的帮助。

1 个答案:

答案 0 :(得分:2)

SQL中,您可以执行以下操作:

select sum(col), avg(col) from table;
相关问题