对我来说,这看起来像是完全的超自然活动。我得到了这个将数据库数据转换为对象的小函数:
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
包含我从调试器复制到 Sqliteman 的 sql 查询。我已经从应用程序中提取了数据库文件。结果如下:
这对我来说看起来很完美。现在让我们看一下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 的看法与此不同。不幸的是,我不知道它应该是什么样子。
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