簡體   English   中英

您的 SQL 語法有錯誤; 檢查與您的 MySQL 服務器版本相對應的手冊,以獲取在 \'keyword")\' 附近使用的正確語法

[英]You have an error in your SQL syntax; check the manual that corresponds to your MySQL server version for the right syntax to use near \'keyword")\'


    url = 'test.com///"asdasdasd'
    name = "test"
    formatURL = url.replace("//","/")
    print(formatURL)

    conn= db.cursor()
    conn.execute("Insert Into website (URL,NAME) VALUES("{}","{}")".format(url,name))
    data_base.commit()

很可能,替換操作未正確完成,我收到以下錯誤。

OUTPUT:

> test.com//"asdasdasd 
> pymysql.err.ProgrammingError: (1064, 'You have an error in your SQL syntax; check the manual that corresponds to your MySQL server version for the right syntax to use near \'test")\' at line 1')

如何將所有“//”字符轉換為“/”字符?

您弄亂了字符串的分隔符。 不要使用str.format()將參數格式化為 sql 字符串,使用參數化查詢:

conn.execute("Insert Into website (URL,NAME) VALUES( %s, %s)", (url,name))

這是混亂:

conn.execute("Insert Into website (URL,NAME) VALUES(" {} "," {} ")".format(url,name))
              111111111111111111111111111111111111111    222    333
                                         unrelated    {}     {}

所有1都是一個字符串,所有2都是另一個字符串,所有3個都是第三個字符串。 兩個{}都是不相關的花括號(?),並且.format(url,name)僅適用於不是有效格式字符串的")" (又名3 )。

只需使用參數化查詢——它們更安全、更容易:

來源: https://xkcd.com/327/許可證

https://xkcd.com/327/

和許多語言的語法提示: https://bobby-tables.com/python

暫無
暫無

聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.

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