在我看来,这完全是超自然活动。我有这个将数据库数据转换为对象的小功能:
public List<BudgetSummary> getMonnthlyBalance() {
List<BudgetSummary> list = new ArrayList<BudgetSummary>();
DbAdapter db = new DbAdapter(context);
SQLiteDatabase sqldb = db.open();
MonthlyBalanceSqlSelect sql = new MonthlyBalanceSqlSelect();
Cursor cursor = sql.getCursor(sqldb);
while (cursor.moveToNext()) {
list.add(new BudgetSummary(cursor));
}
cursor.close();
db.close();
return list;
}
MonthlyBalanceSqlSelect
包含sql查询,我将其从调试器复制到Sqliteman。我已经从应用程序中提取了数据库文件。结果如下:这对我来说看起来很完美。现在让我们看一下
BudgetSummary
构造函数。就像电线一样简单。public BudgetSummary(Cursor cursor) {
incomes = cursor.getDouble(cursor.getColumnIndex("incomes"));
expenses = cursor.getDouble(cursor.getColumnIndex("expenses"));
}
我正在对该构造函数中的每个迭代进行逐步调试。它应该与图片上显示的数据相对应,但不……这里是结果:
1. incomes: 4732.0 - wrong | expenses: -57.59 - wrong | month and year correct
2. incomes: 4657.0 - correct | expenses: -3714.96 - correct | month and year correct
3. incomes: 708.0 - wrong | expenses: -3383.03 - correct | month and year correct
4. incomes: 5669.48 - wrong | expenses: -5669.48 - wrong | month and year correct
5. incomes: 3278.5 - correct | expenses: -2685.91 - wrong | month and year correct
6. incomes: 4612.5 - wrong | expenses: -2786.78 - wrong | month and year correct
and so on...
这是什么法术?我真的不知道...有些值怎么可能是正确的,而另一些却突然消失了?
这是SQL查询。我强烈怀疑Java认为Sqliteman符合this的方式不同。不幸的是,我不知道它的外观。
select strftime('%m', b.date) as month , strftime('%Y', b.date) as year, total(case when b.value >= 0 then b.value else 0 end) as incomes, total(case when b.value < 0 then b.value else 0 end) as expenses from budget b, category c where b.category_id = c.id group by month, year order by year desc, month desc
最佳答案
我强烈怀疑问题是您在Sqliteman和Java代码中假设相同的行顺序。除非您的SQL明确指定顺序,否则您不应假定顺序会相同。我建议您在查询中包括月份和年份,并将它们也包括在BudgetSummary
中,至少用于诊断。