使用python执行mysql,报错了:
name = "AAA'A"
cursor.execute('select * from tb where name=%s',name)
cursor.execute('select * from tb where name=%s',(name))
都会报错
query = query % tuple([db.literal(item) for item in args])
TypeError: not all arguments converted during string formatting
但是以下不会报错:
name = "AAA'A"
cursor.execute('select * from tb where name=%s and %s',(name,1))
python27 如何过滤mysql 单个参数
Since the questioner did not mention which library is used to connect to the database, it is assumed that you are using the source code of
mysqldb
。可以看一下
mysqldb
:You can see that the args parameter is an optional sequence or mapping, that is, the expected type of the args parameter is
list
或者tuple
.Then look back at the input parameters you gave:
So, the solution is simple:
This involves a small detail.
When creating a tuple with only one element, you need to add a comma, otherwise the interpreter will create it as a string.