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()
最有可能的是,替换操作没有正确完成,我得到下面的错误
输出:
> 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字符串,请使用参数化查询:问题就在这里:
所有
1
都是一个字符串,所有2
都是另一个字符串,所有3
都是第三个字符串。两个{}
都是不相关的大括号(?),并且.format(url,name)
仅应用于")"
(aka3
),这不是有效的格式字符串只需使用参数化查询-它们更安全、更简单:
资料来源:https://xkcd.com/327/(License)
许多语言的语法提示:https://bobby-tables.com/python
相关问题 更多 >
编程相关推荐