我正在尝试从SQLite中的表的多个特定列中检索数据,但是使用下面的代码,我不确定为什么,但是不是从两个不同的列中获取数据,而是从第一列中获取数据我将其放入方法中,即“支出”表的“预算”列:-
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;
}
使用上面的代码,我的“ amountfromdb_budget”和“ amountfromdb_foodndrinks”都将被插入到SQLite的“ budget”列中。我的目标是将“预算”列中的值检索到“ amountfromdb_budget”中,将“ foodndrinks”列中的值检索到“ amountfromdb_foodndrinks”中。
下面是我的DatabaseHelper类中的“ getLatestAmount”方法:-
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";
“支出”表的结构如下:-
add_filter( 'woocommerce_default_address_fields' , 'filter_default_address_fields', 20, 1 );
答案 0 :(得分:1)
从获得食物和饮料的量起,您没有覆盖光标。因此,您仍在使用预算光标。因此,将结果从getLatestAmount
保存到latestamount
。
更改
myDb.getLatestAmount(id, temp_category);
至
latestamount = myDb.getLatestAmount(id, temp_category);
因此您的代码应如下所示
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";
// the line below is what you needed to change
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_foodndrinks = latestamount.getDouble(0);
}