简体   繁体   中英

How to get the difference in values from two different tables and feed the value to a variable?

I have two queries that get the sum from two tables as Total_deposit and Total_withdraw .

And I would like to get the difference of Total_deposit and Total_withdraw and feed its value to a variable Balance .

But each time I run the app it says the value for balance is zero via the logd

    Cursor sum_w = mydb.sumwithdraw();
    Cursor sum_d = mydb.sumdeposit();
    if (sum_w.moveToNext() ){
        bal = sum_d.getDouble(sum_d.getColumnIndex("myTotal_deposit")) - sum_w.getDouble(sum_w.getColumnIndex("myTotal_withdraw"));
    }

    if (val<bal){
        Toast.makeText(this, "Insufficeient funds", Toast.LENGTH_SHORT).show();
    }

val is the value retrieved from an Edit_text widget

In your case you only move on sum_w cursor. You have to also move on sum_d cursor.

double sum_w_value = 0d;
double sum_d_value = 0d;
Cursor sum_w = mydb.sumwithdraw();
Cursor sum_d = mydb.sumdeposit();
if (sum_w.moveToNext() ){
    sum_w_value = sum_w.getDouble(sum_w.getColumnIndex("myTotal_withdraw"));
}

if (sum_d.moveToNext() ){
    sum_d_value = sum_d.getDouble(sum_d.getColumnIndex("myTotal_deposit"));
}

bal = sum_d_value - sum_w_value

if (val<bal){
    Toast.makeText(this, "Insufficeient funds", Toast.LENGTH_SHORT).show();
}

By the way this solve your request if there is only one value in each cursor. İf you have more values you have to store each values in list then compare them.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM