【发布时间】:2018-10-25 11:23:11
【问题描述】:
我正在尝试从 SQLite 中表的多个特定列中检索数据,但是使用下面的代码,我不确定为什么,而是从两个不同的列中获取数据,它只是从第一列中获取数据我把它放在我的方法中,即“支出”表的“预算”列:-
String temp_category = "budget";
Cursor latestamount = myDb.getLatestAmount(id, temp_category);
if (latestamount.getCount() == 0) {
Toast.makeText(AddExpenses.this, "No amount found !", Toast.LENGTH_LONG).show();
} else {
latestamount.moveToFirst();
amountfromdb_budget = latestamount.getDouble(0);
}
temp_category = "foodndrinks";
myDb.getLatestAmount(id, temp_category);
if (latestamount.getCount() == 0) {
Toast.makeText(AddExpenses.this, "No amount found !", Toast.LENGTH_LONG).show();
} else {
latestamount.moveToFirst();
amountfromdb_foodndrinks = latestamount.getDouble(0);
}
使用上面的代码,我的“amountfromdb_budget”和“amountfromdb_foodndrinks”都将插入我的 SQLite 的“预算”列中的值。我的目标是将“预算”列中的值检索到“amountfromdb_budget”中,并将“foodndrinks”列中的值检索到“amountfromdb_foodndrinks”中。
下面是我的 DatabaseHelper 类中的“getLatestAmount”方法:-
public Cursor getLatestAmount(String id, String category) {
SQLiteDatabase db = this.getWritableDatabase();
Cursor latestamount = db.rawQuery("SELECT "+category+" FROM spendings WHERE id =" +id , null);
return latestamount;
}
我的“支出”表的结构如下:-
private static final String SPENDING_TABLE = "spendings";
private static final String ID_SPENDING = "id";
private static final String BUDGET = "budget";
private static final String TOTAL_EXPENSES = "total_expenses";
private static final String FOODNDRINKS = "foodndrinks";
private static final String TRAVEL = "travel";
private static final String SHOPPING = "shopping";
private static final String ENTERTAINMENT = "entertainment";
private static final String OTHERS = "others";
private static final String BALANCE = "balance";
【问题讨论】:
-
所以如果我理解正确的话。两次获取预算列数据
-
是的,它两次获取预算列数据,但我想获取预算列数据并将其放入“amountfromdb_budget”,第二次我调用了“myDb.getLatestAmount(id, temp_category)”,我已经更改了“temp_category”到“foodndrinks”以从“foodndrinks”列中获取金额并将该值插入“amountfromdb_foodndrinks”,但不知道如何或正确的方法来做到这一点
-
如何覆盖光标,以便在获得预算后获得食物和饮料的数量?
-
您需要将方法的结果保存到变量 latestamount
标签: android sqlite multiple-columns database-cursor