Java 如何更新SQLite中的特定行?

Java 如何更新SQLite中的特定行?,java,database,sqlite,sql-update,Java,Database,Sqlite,Sql Update,我正在尝试更新用户当前信用,但我不想替换该值,只需从微调器中添加选定的金额并将其添加到用户当前信用?多谢各位 我的数据库代码 类,该类需要在中执行操作: }首先,你应该有一个ID,可以让你找到你感兴趣的用户。进行选择以获取存储在数据库中的当前值。将值解析为整数并添加新货币。最后,更新数据库中的值 public void upDateUser(String ID, String money) { String query = "Select money from TABLE_NAM

我正在尝试更新用户当前信用,但我不想替换该值,只需从微调器中添加选定的金额并将其添加到用户当前信用?多谢各位

我的数据库代码

类,该类需要在中执行操作:


}

首先,你应该有一个ID,可以让你找到你感兴趣的用户。进行选择以获取存储在数据库中的当前值。将值解析为整数并添加新货币。最后,更新数据库中的值

public void upDateUser(String ID, String money) {
        String query = "Select money from TABLE_NAME where ID = " + ID;
        SQLiteDatabase db = this.getWriteableDatabase();
        Cursor cursor = db.rawQuery(selectQuery, null);
        int oldMoney = 0;
        if (cursor.moveToFirst()) {         
            oldMoney = Integer.parseInt(cursor.getString(0)); //Cause we get only from money column
        }
        ContentValues cvUpdate = new ContentValues();
        cvUpdate.put(KEY_CREDITS, oldMoney  + money);
        String filter = "UID" + "=" + ID;
        db.update(DATABASE_TABLE, cvUpdate, filter, null);
    }

当然,您必须检查游标是否正好返回一行,并执行其他一些检查。

@user3408604如果您只有一个用户,使用SharedReferences而不是使用数据库可能是更好的主意。数据库应该包含多个用户/行。仅为一个用户维护数据库对程序员和应用程序来说都是太多的工作。在你的设计中考虑
package com.example.parkangel;  

public class Balance extends Activity implements OnClickListener{

Button add;
TextView display;
Spinner spinner3;
Integer[] money = new Integer[] {1, 2, 5, 10};  

protected void onCreate(Bundle savedInstanceState) {                
    super.onCreate(savedInstanceState);
    setContentView(R.layout.balance_layout);
    TextView tv = (TextView) findViewById(R.id.firstn);
    UDbHelper db = new UDbHelper(this);
    db.open();
    String data = db.getData();
    //db.addUser();
    db.close();
    tv.setText(data);

    ArrayAdapter<Integer> adapter3 = new ArrayAdapter<Integer>(Balance.this,   
                                     android.R.layout.simple_spinner_item, money);

    spinner3 = (Spinner) findViewById (R.id.moneytoadd);
    spinner3.setAdapter(adapter3);

    add = (Button) findViewById(R.id.topup);
    add.setOnClickListener(this);
    //add = (Button) findViewById(R.id.topup);      
}

public void onClick(View arg0)
{
    switch (arg0.getId()){
    case R.id.topup:

        boolean work = true;
        try{
        String money = spinner3.getContext().toString();

        UDbHelper ud = new UDbHelper(this);
        ud.open();
        ud.upDateUser(money);
        ud.close();

    }catch (Exception e){
        work = false;
        String error = e.toString();
        Dialog d = new Dialog(this);
        d.setTitle("Unable To TopUp!");
        TextView dg = new TextView(this);
        dg.setText(error);
        d.setContentView(dg);
        d.show();
}finally{
    if(work){
        Dialog d = new Dialog(this);
        d.setTitle("You Have ToppedUp!");
        TextView dg = new TextView(this);
        dg.setText("TopUp Successful");
        d.setContentView(dg);
        d.show();
            }
        }   
        break;
}
}

public void updateActivity(View view){
    Intent book = new Intent(Balance.this, BookTicket.class);
    startActivity(book);
}

public void addBalance(View view){
    Intent addB = new Intent(Balance.this, Balance.class);
    startActivity(addB);
}

public void doUpdate(View view){
    Intent upd = new Intent(Balance.this, UpdateTicket.class);
    startActivity(upd);
}
public void upDateUser(String ID, String money) {
        String query = "Select money from TABLE_NAME where ID = " + ID;
        SQLiteDatabase db = this.getWriteableDatabase();
        Cursor cursor = db.rawQuery(selectQuery, null);
        int oldMoney = 0;
        if (cursor.moveToFirst()) {         
            oldMoney = Integer.parseInt(cursor.getString(0)); //Cause we get only from money column
        }
        ContentValues cvUpdate = new ContentValues();
        cvUpdate.put(KEY_CREDITS, oldMoney  + money);
        String filter = "UID" + "=" + ID;
        db.update(DATABASE_TABLE, cvUpdate, filter, null);
    }