2013-06-25 2 views
1

사용자가 소득이나 비용을 입력 할 수있는 간단한 금융 응용 프로그램을 만들고 있습니다. 데이터베이스 내에서 숫자를 더하거나 뺄 때 "전체"양을 어떻게 바꿀 수 있는지는 어디에서도 찾을 수 없습니다. 내가 설명 할 수있는 가장 쉬운 방법은 다음과 같습니다.안드로이드 용 SQLite에서 숫자를 더하거나 빼는 방법은 무엇입니까?

사용자가 10 달러의 수입을 입력합니다. 따라서이 10 개를 데이터베이스에 추가합니다. 사용자는 - $ 5의 비용을 입력합니다. 따라서 데이터베이스에 추가 할 수도 있습니다.

최종 결과는 총 5 달러 여야하지만 어떻게해야합니까?

전 SQLite를 사용 해본 적이 없어서 완전히 고생했습니다. 감사합니다

+3

"데이터베이스 내부"값을 변경하지 마십시오. 값을 읽고 변경 한 다음 데이터베이스를 업데이트합니다. 웹에는 수천 개의 SQLite 튜토리얼이 있습니다. 예를 들어, 매번 업데이트하는 누적 합계를 유지하거나 더 나은 쿼리를 사용하여 합계를 얻을 수 있습니다. – Simon

답변

1
당신은 단지에이 명령을 발사하여 SQL
는) 새로운 총을 추가 안드로이드 프로그래밍에서 SQLite 데이터베이스
B의 값)을 얻거나) 그 (것)들에게 Update
C를 뺄 Select를 사용 할 수

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

} 
관련 문제