添加到当前值的SQLite [英] Adding on to the current value SQLite
问题描述
我有谁拥有设定在'5'的学分初始值的用户。现在,我想创建,这将允许用户添加到这个数字的方法,例如, 2,这将随后在数据库列更新该行成为7.
不过,我不能让我的当前的方法来做到这一点。
当前方法:
公共无效UpdateUser两个(串钱)
{
字符串selectQuery =SELECT+ KEY_CREDITS +由+
DATABASE_TABLE +,其中+ KEY_ROWID +=+ 0;
SQLiteDatabase分贝= this.getWritableDatabase();
光标C = db.rawQuery(selectQuery,NULL);
INT oldMoney = 0;
如果(c.moveToFirst())
{
oldMoney =的Integer.parseInt(c.getString(4));
}
ContentValues cvUpdate =新ContentValues();
cvUpdate.put(KEY_CREDITS,(oldMoney +钱));
字符串过滤器= KEY_ROWID +=+ 0;
db.update(DATABASE_TABLE,cvUpdate,过滤器,NULL);
}
我随后致电上述方法在另一个活动:
公共无效的onClick(查看arg0中)
{
开关(arg0.getId()){
案例R.id.topup: 布尔的工作= TRUE;
尝试{
串钱= spinner.getSelectedItem()的toString()。 UDbHelper UD =新UDbHelper(本);
ud.open();
ud.upDateUser(钱);
ud.close();
更改以下步骤才能oldmoney的值
oldMoney = c.getInt(c.getColumnIndex(KEY_CREDITS));
你也不能添加字符串来得到一个整的总价值。找到前总这样的价值的钱转换为整数
INT钱=的Integer.parseInt(钱);
如果您有KEY_CREDITS列字符串数据类型,然后找到后总你应该把它转换回字符串更新前
cvUpdate.put(KEY_CREDITS,将String.valueOf(oldMoney +钱));
I have a user who has a starting value for their credits set at '5'. Now I want to create a method which will allow the user to add on to that number, e.g. 2. This will then update the row in the database column to become 7.
However I cant get my current method to do this.
Current method:
public void upDateUser(String money)
{
String selectQuery = "SELECT " + KEY_CREDITS + " from " +
DATABASE_TABLE + " where " + KEY_ROWID + " = " + 0;
SQLiteDatabase db = this.getWritableDatabase();
Cursor c = db.rawQuery(selectQuery, null);
int oldMoney = 0;
if (c.moveToFirst())
{
oldMoney = Integer.parseInt(c.getString(4));
}
ContentValues cvUpdate = new ContentValues();
cvUpdate.put(KEY_CREDITS, (oldMoney + money));
String filter = KEY_ROWID + "=" + 0;
db.update(DATABASE_TABLE, cvUpdate, filter, null);
}
I then call the above method in an another activity:
public void onClick(View arg0)
{
switch (arg0.getId()){
case R.id.topup:
boolean work = true;
try{
String money = spinner.getSelectedItem().toString();
UDbHelper ud = new UDbHelper(this);
ud.open();
ud.upDateUser(money);
ud.close();
Change the following step to get the value of oldmoney
oldMoney = c.getInt(c.getColumnIndex(KEY_CREDITS));
Also you cannot add strings to get an integer total value. So convert the value money to integer before finding the total
int money =Integer.parseInt(money);
If you have a string data type for KEY_CREDITS column then after finding the total you should convert it back to string before updating
cvUpdate.put(KEY_CREDITS, String.valueOf(oldMoney + money));
这篇关于添加到当前值的SQLite的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!