[英]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()
Most likely, Replace operation was not done correctly and I get the error below.很可能,替换操作未正确完成,我收到以下错误。
OUTPUT: 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')
How can I convert all "//" characters to a '/' character?如何将所有“//”字符转换为“/”字符?
You messed up the delimiters of your string.您弄乱了字符串的分隔符。 Don't use
str.format()
to format parameters into sql strings, use parametrized queries:不要使用
str.format()
将参数格式化为 sql 字符串,使用参数化查询:
conn.execute("Insert Into website (URL,NAME) VALUES( %s, %s)", (url,name))
Here is the mess-up:这是混乱:
conn.execute("Insert Into website (URL,NAME) VALUES(" {} "," {} ")".format(url,name))
111111111111111111111111111111111111111 222 333
unrelated {} {}
all the 1
are one strings, all the 2
are another one and all 3
the third string.所有
1
都是一个字符串,所有2
都是另一个字符串,所有3
个都是第三个字符串。 Both {}
are unrelated curly braces(?) and the .format(url,name)
is only applied to ")"
(aka 3
) which is not a valid format string.两个
{}
都是不相关的花括号(?),并且.format(url,name)
仅适用于不是有效格式字符串的")"
(又名3
)。
Simply use parametrized queries - they are safer and much easier:只需使用参数化查询——它们更安全、更容易:
Source: https://xkcd.com/327/ ( License )来源: https://xkcd.com/327/ (许可证)
and syntax hints for lots of languages: https://bobby-tables.com/python和许多语言的语法提示: https://bobby-tables.com/python
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.