如果buyid(主键)是特定值,我试图更新列数。
UPDATE portfolio set amount=40 WHERE buyid=3
我使用JDBC和MySql,每次尝试执行语句时都会遇到以下异常:
com.mysql.jdbc.exceptions.jdbc4.MySQLSyntaxErrorException:你有一个 SQL语法错误;查看与您的手册相对应的手册 MySQL服务器版本的正确语法使用附近' buyid = 3'在 第1行
投资组合的表格结构:
buyid int
username varchar
stockname varchar
priceperstock float
amount int
Javasourcecode:
public void sellStock(int buyid, int amount, float currentprice, String user) {
...
try {
stmt = this.conn.createStatement();
System.out.println(fetchedamount);
System.out.println("UPDATE portfolio
SET amount=" + fetchedamount
+ " WHERE buyid=" + buyid);
stmt.execute("UPDATE stockman.portfolio
SET amount=" + fetchedamount
+ "WHERE buyid=" + buyid+"");
// update capital
newmoney = amount * currentprice + oldmoney;
} catch (SQLException ee) {
ee.printStackTrace();
}
答案 0 :(得分:7)
在stmt.execute()
您生成的查询就像是
"UPDATE stockman.portfolio set amount=23WHERE buyid=54 "
此处23Where
是一整个字符串,因此您必须在这两个字符串之间留出空格。
在“和 Where 之间留出空格,如下所示:
" WHERE buyid="
删除最后一个+""
答案 1 :(得分:0)
只需在解决问题的地方之前添加空格。
stmt.execute("UPDATE stockman.portfolio
SET amount=" + fetchedamount
+ " WHERE buyid=" + buyid+"");
答案 2 :(得分:0)
补充stm.execute
行:
stmt.execute("UPDATE stockman.portfolio SET amount=" + fetchedamount + " WHERE buyid=" + buyid+" ");
或者:
stmt.execute("UPDATE stockman.portfolio SET amount='" + fetchedamount + "' WHERE buyid='" + buyid+"' ");