如何在SQLite for android中添加和减去数字?

如何在SQLite for android中添加和减去数字?,android,sqlite,function,finance,Android,Sqlite,Function,Finance,我正在创建一个简单的金融应用程序,用户可以在其中输入收入或费用。我找不到任何地方可以通过在数据库中添加或减去数字来更改“总数”。我能解释的最简单的方法是: 用户输入10美元的收入:所以我会将这10美元添加到数据库中。 用户输入的费用为-$5:所以我还要将其添加到数据库中 最终结果应该是总共5美元,但我该怎么做呢 我完全被卡住了,因为我以前从未使用过SQLite。谢谢您只需在SQL a) 使用Select从SQLite数据库中获取值 b) 在Android编程中,添加或减去它们 c) 更新将新总数

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

用户输入10美元的收入:所以我会将这10美元添加到数据库中。 用户输入的费用为-$5:所以我还要将其添加到数据库中

最终结果应该是总共5美元,但我该怎么做呢


我完全被卡住了,因为我以前从未使用过SQLite。谢谢

您只需在
SQL

a) 使用
Select
SQLite
数据库中获取值
b) 在Android编程中,添加或减去它们
c)
更新
将新总数输入
数据库

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();

}

只需在
SQL

a) 使用
Select
SQLite
数据库中获取值
b) 在Android编程中,添加或减去它们
c)
更新
将新总数输入
数据库

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();

}
您不会在“数据库内部”更改值。读取值,更改它们,然后更新数据库。网上有成千上万的SQLite教程。例如,您可以维护一个每次更新的运行总数,或者更好,只需使用一个查询来获取总数。您不会在“数据库内部”更改值。读取值,更改它们,然后更新数据库。网上有成千上万的SQLite教程。例如,您可以维护一个每次更新的运行总数,或者更好,只需使用查询来获取总数。