如何在Android中为SQLite添加和减去数字? [英] How do I add and subtract numbers in SQLite for android?

查看:209
本文介绍了如何在Android中为SQLite添加和减去数字?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我正在创建一个简单的金融应用,用户可以输入收入或费用。我找不到任何地方,我可以通过在数据库中添加或减去数字来更改总计金额。我可以解释的最简单的方法是:

用户输入$ 10的收入:所以我会将10添加到数据库中。
用户输入 - $ 5的费用:所以我也将其添加到数据库中

最终结果应该是$ 5作为总数,但是我如何做到这一点?



我完全陷入困境,因为我以前从未使用过SQLite。谢谢

解决方案

你可以简单地通过在 SQL

a)使用选择以从 SQLite 数据库
$ b $中获取值bb)在Android编程中添加它们或将它们减去

c) Update 将新的Total添加到数据库中

  public void updateExpense(decimal Expense,String Condition){
double current = 0;
db = this.getReadableDatabase();
String selectQuery =select id,total from+ TABLE_YourTable;
Cursor cursor = db.rawQuery(selectQuery,null);
int RowID = 0;
if(cursor.moveToFirst()){
current = Double.parseDouble(cursor.getString(1));
RowID = Integer.parseInt(cursor.getString(0));
}
///现在我们使用条件 - >如果条件为正,则意味着增加...如果条件为负,则意味着
////减去
if(Condition.equals(positive){
current + = Expense;
} else {
current = current - Expense;
}
cursor.close();
db.close();


//更新到SQLite
db = this.getReadableDatabase();
ContentValues values = new ContentValues();
values.put(total,current);

db.update(TABLE_YourTable,values,KEY_ID +=?,new String [] {String.valueOf(RowID)});
db.close();

}


I'm creating a simple financial app where the user can input an income or expense. I cannot find anywhere how I can change the "total" amount by adding or subtracting numbers inside the database. The easiest way I can explain it is:

user enters an income of $10 : So I would add that 10 into the database. user enters an expense of -$5 : so i would also add that into the database

the end result should be $5 as the total, but how do I do this?

I'm completely stuck as I've never use SQLite before. Thanks

解决方案

You can do that simply by firing 2 commands on SQL
a) Use Select to get the value from the SQLite Database
b) In Android programming add them or subtract them
c) Update the new Total into the database

public void updateExpense(decimal Expense,String Condition) {
    double current = 0;
    db = this.getReadableDatabase();
    String selectQuery = "select id, total from  " + TABLE_YourTable ;
    Cursor cursor = db.rawQuery(selectQuery, null);
    int RowID=0;
    if (cursor.moveToFirst()) {
            current=  Double.parseDouble(cursor.getString(1));
            RowID= Integer.parseInt(cursor.getString(0));
    }
    /// Now we use condition --> if condition is positive it mean add ... if condition is negative it means 
    ////subtract
    if(Condition.equals("positive"){
            current += Expense;
    }else {
            current =current - Expense; 
    }
    cursor.close();
    db.close();


    //Your Update to SQLite
    db = this.getReadableDatabase();
    ContentValues values = new ContentValues();
    values.put(total , current );

    db.update(TABLE_YourTable , values, KEY_ID + " = ?", new String[] { String.valueOf(RowID) });
    db.close();

}

这篇关于如何在Android中为SQLite添加和减去数字?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆