简体   繁体   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()

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/许可证

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.

相关问题 您的 SQL 语法有错误; 检查与您的 MySQL 服务器版本相对应的手册,以获取在 ') 附近使用的正确语法 - 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 ') “你的SQL语法有错误; 查看与您的MySQL服务器版本对应的手册,以便在''第1行'附近使用正确的语法 - “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 '' at line 1” 警告:您的SQL语法有错误; 查看与您的MySQL服务器版本对应的手册,以便在附近使用正确的语法 - Warning: 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 您的 SQL 语法有错误; 检查与您的 MySQL 服务器版本相对应的手册,以获取在 '*) 附近使用的正确语法 - 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 '*) 您的SQL语法有错误; 检查与您的MariaDB服务器版本对应的手册,以便在第1行附近使用正确的语法, - You have an error in your SQL syntax; check the manual that corresponds to your MariaDB server version for the right syntax to use near at line 1", 1064(42000):您的 SQL 语法有错误; 检查与您的 MySQL 服务器版本相对应的手册,以获取正确的语法使用 - 1064 (42000): You have an error in your SQL syntax; check the manual that corresponds to your MySQL server version for the right syntax to use 修复“查看与您的 MySQL 服务器版本相对应的手册,以获取在 '%s 附近使用的正确语法” - FIX “Check the manual that corresponds to your MySQL server version for the right syntax to use near '%s” _mysql_exceptions错误(1064,“检查与MySQL服务器版本对应的手册,以便在'default'附近使用正确的语法)VALUES - _mysql_exceptions error(1064, "check the manual that corresponds to your MySQL server version for the right syntax to use near 'default) VALUES 您的 SQL 语法有错误; 检查与您的 MySQL 服务器 PYTHON 相对应的手册 - You have an error in your SQL syntax; check the manual that corresponds to your MySQL server PYTHON 42000您的SQL语法错误; 检查与您的MySQL服务器相对应的手册 - 42000 You have an error in your SQL syntax; check the manual that corresponds to your MySQL server
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM