[英]Whats wrong with this SQL clause?
I'm trying to update the column amount if the buyid (primary key) is a specific value. 如果buyid(主键)是特定值,我正在尝试更新列数。
UPDATE portfolio set amount=40 WHERE buyid=3
I work with JDBC and MySql, everytime I try to execute the statement i get the following exception: 我使用JDBC和MySql,每次尝试执行该语句时,都会收到以下异常:
com.mysql.jdbc.exceptions.jdbc4.MySQLSyntaxErrorException: You have an error in your SQL syntax; com.mysql.jdbc.exceptions.jdbc4.MySQLSyntaxErrorException:SQL语法有错误; check the manual that corresponds to your MySQL server version for the right syntax to use near 'buyid=3' at line 1 检查与您的MySQL服务器版本相对应的手册,以在第1行的'buyid = 3'附近使用正确的语法
table structure of portfolio: 投资组合表结构:
buyid int
username varchar
stockname varchar
priceperstock float
amount int
Javasourcecode: Java源代码:
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();
}
At stmt.execute()
Your generated query is like 在stmt.execute()
您生成的查询就像
"UPDATE stockman.portfolio set amount=23WHERE buyid=54 "
Here 23Where
is one whole string so you have to give space between these two. 这里23Where
是一个完整的字符串,因此您必须在两者之间23Where
空格。
Give space between " and Where like the following: 给予“至何处类似下面的空间:
" WHERE buyid="
And remove the last +""
并删除最后一个+""
Just add space before Where it will solve the problem. 只需在解决问题的位置之前添加空间即可。
stmt.execute("UPDATE stockman.portfolio
SET amount=" + fetchedamount
+ " WHERE buyid=" + buyid+"");
Repleace the stm.execute
line with: stmt.execute("UPDATE stockman.portfolio SET amount=" + fetchedamount + " WHERE buyid=" + buyid+" ");
用以下stm.execute
行: stmt.execute("UPDATE stockman.portfolio SET amount=" + fetchedamount + " WHERE buyid=" + buyid+" ");
Or: stmt.execute("UPDATE stockman.portfolio SET amount='" + fetchedamount + "' WHERE buyid='" + buyid+"' ");
或者: stmt.execute("UPDATE stockman.portfolio SET amount='" + fetchedamount + "' WHERE buyid='" + buyid+"' ");
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.