从SQLite数据库返回错误的平均值

时间:2013-07-21 05:27:22

标签: android sqlite average

我试图从我的SQLite数据库中获得平均最后5个(非空)条目。但不知何故,我没有得到预期的答案。

继承人查询

    db.execSQL( "CREATE TABLE "+FUEL_DATABASE_TABLE +" (" +
                            KEY_ROW_ID + " INTEGER PRIMARY KEY AUTOINCREMENT, " +
                            KEY_KM + " LONG NOT NULL, "+
                            KEY_FUEL_QTY + " INTEGER NOT NULL, "+
                            KEY_FUEL_PRICE + " DOUBLE NOT NULL, "+
                            KEY_TOTAL_COST + " DOUBLE NOT NULL, "+
                            KEY_MILEAGE + " DOUBLE NOT NULL, "+
                            KEY_DATE + " DATE NOT NULL, "+
                            KEY_TANK_FULL + " TEXT);"
                            );

    public long createEntry(FuelExpense fe) 
        {
            // TODO Auto-generated method stub


            ContentValues cv = new ContentValues();
            cv.put(KEY_KM, fe.getKm());
            cv.put(KEY_FUEL_QTY, fe.getFuelQty());
            cv.put(KEY_FUEL_PRICE,fe.getFuelPrice());
            cv.put(KEY_TOTAL_COST, fe.getTotalCost());
            cv.put(KEY_MILEAGE, fe.getMileage());
            cv.put(KEY_DATE, fe.getFuelFilledDate());
            cv.put(KEY_TANK_FULL, fe.getTankFullstatus());
            return ourDatabase.insert(FUEL_DATABASE_TABLE, null, cv);
        }





        public String getData()
        {
            // TODO Auto-generated method stub
            String [] columns = new String[]{KEY_ROW_ID, KEY_KM, KEY_FUEL_QTY, KEY_FUEL_PRICE, KEY_TOTAL_COST, KEY_MILEAGE, KEY_DATE,KEY_TANK_FULL};
            fuelCursor =  ourDatabase.query(FUEL_DATABASE_TABLE, columns, null, null, null, null, null);
            String resultSQL= "";

            while(fuelCursor.moveToNext())
            {
                resultSQL = resultSQL + fuelCursor.getString(0) + "         " + fuelCursor.getString(1) + "         " + fuelCursor.getString(2) +"           "+ fuelCursor.getString(3) + "          " + fuelCursor.getString(4) + "          " + fuelCursor.getString(5) + "         " + fuelCursor.getString(6) + "        " + fuelCursor.getString(7) + "\n ";  
            }
            return resultSQL;
        }
 public void getAvgMileage(Prediction pd) 
    {
            predictionCursor = ourDatabase.rawQuery("SELECT ROUND(AVG(_mileage)) FROM fuel_table WHERE _mileage IS NOT NULL ORDER BY _id DESC LIMIT 5", null);
                        if(predictionCursor!=null && predictionCursor.getCount()>0)
                        {
                                predictionCursor.moveToLast();
                                findAvgMileage = predictionCursor.getDouble(0);
                                pd.setpredictionMileage(findAvgMileage);

                        }

    The value obtained from predictionCursor needs to be used for further calculations like so,

            if(ETPredictFuelQty.hasFocus())
                        {
                            if(predictKm!=0)
                            {
                                predictFuetlQty =predictKm/predictionMileage;
                                ETPredictFuelQty.setText(new DecimalFormat("##.##").format(predictFuetlQty));
                            }

                        }

_mileage的值为10,10,5,3.33,7.5,其avg应为predictAvgMileage = 7.166

我输入predictKm = 1000

的值

SO predictFuelQty = predictKm / predictAvgMileage = 139.54(预期答案)

但我得到的预测燃料数量= 166.67

任何人都可以帮我找出什么是错的。 还有继承SQL日志

SELECT name, sql FROM sqlite_master WHERE type='table' ;
PRAGMA TABLE_INFO(android_metadata);
PRAGMA TABLE_INFO(sqlite_sequence);
PRAGMA TABLE_INFO(fuel_table);
SELECT name, sql FROM sqlite_master WHERE type='index' 
SELECT rowid, *  FROM android_metadata ORDER BY rowid; 
SELECT name, sql FROM sqlite_master WHERE type='table' ;
PRAGMA TABLE_INFO(android_metadata);
PRAGMA TABLE_INFO(sqlite_sequence);
PRAGMA TABLE_INFO(fuel_table);
SELECT name, sql FROM sqlite_master WHERE type='index' 
SELECT rowid, *  FROM android_metadata ORDER BY rowid; 
SELECT rowid, *  FROM fuel_table ORDER BY rowid; 

3 个答案:

答案 0 :(得分:1)

我不认为你得到166.67,我认为你实际上得到142.85(142.86,我不确定Java的DecimalFormat如何处理舍入和截断。)

为什么我会这么想?好吧,你的SQL说:

SELECT ROUND(AVG(_mileage)) ...

所以平均值是7.166,当你绕它时它变成7.0。然后1000/7.0会为您提供142.8571...

在任何情况下,我认为您只想从SQL中删除ROUND()调用。如果你真的得到166.67那么你会想要检查你的数据是否真的是你在ORDER和LIMIT之后的想法,或者在你计算的其他地方寻找一个bug。

答案 1 :(得分:1)

我希望得到更多来自你的意见。它会告诉你这里有什么问题,如果没有,我可以解决你的问题。

1)在问题中添加表格模式。(这可能是你的问题)

2)在您尝试从数据库获取平均值之前,尝试获取并打印数据库中可用的所有值。打印它们,在问题中添加日志。

3)打印从DB获得的AVG值。

findAvgMileage = predictionCursor.getDouble(0); // print findAvgMileage in the logs

完成后添加评论,以便我得到通知。

P.S: - 这不是完整的答案,由于篇幅我无法将其添加为评论。一旦OP更新问题,将更新答案更多输入

答案 2 :(得分:1)

您的问题是LIMIT子句适用于查询的结果,该查询已经是单个记录。 换句话说,查询首先计算所有非NULL记录的平均值,然后确保AVG函数不会返回超过五个值。

尝试使用子查询:

SELECT AVG(_mileage)
FROM (SELECT _mileage
      FROM fuel_table
      WHERE _mileage IS NOT NULL
      ORDER BY _id DESC
      LIMIT 5)