我更喜欢在攻击MySQL绑定的python代码时使用命名占位符,但似乎我无法使用IN
子句得到它。一个例子:
con = MySQLdb.connect(db='test', user='test')
cur = con.cursor()
三个太简单的例子:
# (#1) works fine, but I want placeholders.
cur.execute( """ update test
set i = 999
where SNO in (1, 2) """)
# (#2) works fine too, but still not enough placeholders.
cur.execute( """ update test
set i = %(i)s
where SNO in (1, 2) """, {'i' : 999})
# (#3) works, but did not pass the beauty check...
cur.execute( """ update test
set i = %(i)s
where SNO in ( %(a)s, %(b)s ) """, {'i' : 99,
'a' : 1,
'b' : 2})
这是我真正想要的,但它失败了:操作数应该包含1列
# (#4) This one fails with: _mysql_exceptions.OperationalError: (1241, 'Operand should contain 1 column(s)')
cur.execute( """ update test
set i = %(i)s
where SNO in ( %(foo)s ) """, {'i' : 999,
'foo' : [1, 2]})
显然我需要更多魔法。简单地将问题移到应用程序中,在python中实现循环很简单,但我宁愿避免这种情况。
啊,是的,表现也很重要。
答案 0 :(得分:3)
MySQLdb
已经为您处理序列的转义:
>>> con = MySQLdb.connect(db='test')
>>> con.literal([1,2,3])
('1', '2', '3')
>>> cur = con.cursor()
>>> cur.execute("select * from test where id in %(foo)s", {'foo': [1,2,3]})
3L
>>> cur._executed
"select * from test where id in ('1', '2', '3')"
因此,通过删除占位符周围的括号,它应该可以工作 - 但仅适用于具有多个元素的序列,因为单个元素的格式如下:
>>> con.literal([1])
('1',)
插入到SQL查询中,尾随逗号使其成为非法SQL。
为了解决这个问题,您还可以定义自己的转换器,将自定义类型转换为您喜欢的表示形式:
import MySQLdb.converters
conv = MySQLdb.converters.conversions.copy()
class CustomList(list):
def __init__(self, *items):
super(CustomList, self).__init__(items)
conv[CustomList] = lambda lst, conv: "(%s)" % ', '.join(str(item) for item in lst)
con = MySQLdb.connect(db='test', conv=conv)
cur = con.cursor()
cur.execute('select * from test where id in %(foo)s', {'foo': CustomList(0, 1, 2)})
print cur._executed
select * from test where id in (0, 1, 2)
这样,列表项周围的引号就消失了。
只需替换list
的转换器,但这会改变所有列表的行为并可能引入漏洞。对于包含字符串的列表,上述格式化列表的方式不安全,因为它不会转义特殊字符。
为此,您必须以递归方式转义列表中的所有项目:
>>> ...
>>> conv[list] = lambda lst, cv: "(%s)" % ', '.join(cv[type(item)](item, cv) for item in lst)
>>> con = MySQLdb.connect(..., conv=conv)
>>> con.literal([1, "it's working...", 2])
"(1, 'it\\'s working...', 2)"