[英]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 ')
[英]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://bobby-tables.com/python
聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.