簡體   English   中英

這個SQL子句怎么了?

[英]Whats wrong with this SQL clause?

如果buyid(主鍵)是特定值,我正在嘗試更新列數。

UPDATE portfolio set amount=40 WHERE buyid=3

我使用JDBC和MySql,每次嘗試執行該語句時,都會收到以下異常:

com.mysql.jdbc.exceptions.jdbc4.MySQLSyntaxErrorException:SQL語法有錯誤; 檢查與您的MySQL服務器版本相對應的手冊,以在第1行的'buyid = 3'附近使用正確的語法

投資組合表結構:

buyid int
username varchar
stockname varchar
priceperstock float
amount int

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();
    }

stmt.execute()您生成的查詢就像
"UPDATE stockman.portfolio set amount=23WHERE buyid=54 "

這里23Where是一個完整的字符串,因此您必須在兩者之間23Where空格。

給予何處類似下面的空間:

" WHERE buyid=" 

並刪除最后一個+""

只需在解決問題的位置之前添加空間即可。

 stmt.execute("UPDATE stockman.portfolio
                  SET amount=" + fetchedamount
               + " WHERE buyid=" + buyid+"");

用以下stm.execute行: 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.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM