public void update(View v){String amount1 = ct2.getText().toString(); //amount
String barcode1 = ct1.getText().toString(); //barcode
ct1.setText("");//barcode
ct2.setText("");//amount
db.execSQL("update mytable set amount = '"+amount1+"' wherebarcode='"+barcode1+"' ");
Toast.makeText(this, "values updated successfully.", Toast.LENGTH_LONG).show();
this.finish();
}
我正在从android studio 创建应用程序
我尝试从mytable
更新已存储在“'mydb'
”中的数据
我没有使用 DBHelper ,所以我看不到确切的表格。
只是SQLite的即时代码。
我可以插入新数据,但我无法UPDATE
数据
正如我的代码似乎正确。
运行应用程序时一切都没有错误,
我通过使用移动相机扫描获得barcode
值
所以在我的桌子上
NAME AMOUNT BARCODE
(小)
吐司表明它已成功更新
但是当我去检查时
SELECT * from mytable
AMOUNT仍然与插入时相同
请帮帮我
答案 0 :(得分:1)
请查看您的查询:
db.execSQL("update mytable set amount = '"+amount1+"' wherebarcode='"+barcode1+"' ");
在where子句后面没有空格。编辑并重试
答案 1 :(得分:0)
你有两个问题。第一个是WHERE关键字和列名之间没有空格。第二个是,正如您所发现的那样,无论UPDATE查询的结果如何,TOAST都会说UPDATE成功。
要修复第一个,你会改变
db.execSQL("update mytable set amount = '"+amount1+"' wherebarcode='"+barcode1+"' ");
到(即添加空格): -
db.execSQL("update mytable set amount = '"+amount1+"' where barcode='"+barcode1+"' ");
但是,由于execSQL
方法未返回结果,因此第二个问题仍将存在。修复方法是使用update
方法,该方法将返回受影响/更新的行数。
因此你可以使用: -
ContentValuse cv = new ContentValues();
cv.put("amount",amount1); // Column to update and value to update the column to.
if (db.update("mytable",cv,"barcode=?",new String[]{barcode1}) > 0) {
Toast.makeText(this, "values updated successfully.", Toast.LENGTH_LONG).show();
} else {
Toast.makeText(this, "not updated.", Toast.LENGTH_LONG).show();
}
您可能希望看一下: -