Warning: file_get_contents(/data/phpspider/zhask/data//catemap/8/mysql/71.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
Python 您的SQL语法有错误;查看与您的MySQL服务器版本相对应的手册,以了解使用near\'的正确语法;关键词";)\';_Python_Mysql_Python 3.x - Fatal编程技术网

Python 您的SQL语法有错误;查看与您的MySQL服务器版本相对应的手册,以了解使用near\'的正确语法;关键词";)\';

Python 您的SQL语法有错误;查看与您的MySQL服务器版本相对应的手册,以了解使用near\'的正确语法;关键词";)\';,python,mysql,python-3.x,Python,Mysql,Python 3.x,最有可能的是,替换操作没有正确完成,我得到下面的错误 输出: 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)

最有可能的是,替换操作没有正确完成,我得到下面的错误

输出:


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

如何将所有“/”字符转换为“/”字符?

您弄乱了字符串的分隔符。不要使用
str.format()
将参数格式化为sql字符串,请使用参数化查询:

> 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')
问题就在这里:

conn.execute("Insert Into website (URL,NAME) VALUES( %s, %s)", (url,name))
所有
1
都是一个字符串,所有
2
都是另一个字符串,所有
3
都是第三个字符串。
{}
都是不相关的大括号(?),而
.format(url,name)
仅应用于
(又称
3
),它不是有效的格式字符串

只需使用参数化查询-它们更安全、更简单:

资料来源:()


许多语言的语法提示:

不要为sql字符串设置值的格式:使用参数化查询
conn.execute(“插入网站(URL,名称)值(%s,%s)”,(URL,名称))
哦,替换操作正确。正如Patrick所指出的,您只是有一个SQL注入错误。这个解决方案能解决这个问题吗?那么这是一个永久性的解决方案吗?它在不同的SQL注入中工作吗?可能是的-字符串在{}之前/之后也使用“来分隔自身和使用”,因此python对什么是字符串、什么代码和什么不是感到困惑。这只是将参数传递到sql中的一种方式——您从不使用格式来给出值。您应该从这一行中得到一个Python语法错误。
conn.execute("Insert Into website (URL,NAME) VALUES(" {} "," {} ")".format(url,name))
              111111111111111111111111111111111111111    222    333
                                         unrelated    {}     {}