我试图从我的 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,其平均值应为 predictAvgMileage=7.166
我输入 predictKm =1000 的值
所以 predictFuelQty = predictKm/predictAvgMileage = 139.54(预期答案)
但我得到 predictFuelQty = 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;