Skip to content

Commit 44f8b4d

Browse files
committed
Merge pull request #36 from Multiposting/master
Fix the conversion of list or tuple args to SQL.
2 parents a7c3ce4 + 8096d8c commit 44f8b4d

File tree

2 files changed

+17
-4
lines changed

2 files changed

+17
-4
lines changed

MySQLdb/converters.py

Lines changed: 5 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -129,13 +129,16 @@ def char_array(s):
129129
def array2Str(o, d):
130130
return Thing2Literal(o.tostring(), d)
131131

132+
def quote_tuple(t, d):
133+
return "(%s)" % (','.join(escape_sequence(t, d)))
134+
132135
conversions = {
133136
IntType: Thing2Str,
134137
LongType: Long2Int,
135138
FloatType: Float2Str,
136139
NoneType: None2NULL,
137-
TupleType: escape_sequence,
138-
ListType: escape_sequence,
140+
TupleType: quote_tuple,
141+
ListType: quote_tuple,
139142
DictType: escape_dict,
140143
InstanceType: Instance2Str,
141144
ArrayType: array2Str,

MySQLdb/cursors.py

Lines changed: 12 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -180,7 +180,11 @@ def execute(self, query, args=None):
180180
if isinstance(query, unicode):
181181
query = query.encode(db.unicode_literal.charset)
182182
if args is not None:
183-
query = query % db.literal(args)
183+
if isinstance(args, dict):
184+
query = query % dict((key, db.literal(item))
185+
for key, item in args.iteritems())
186+
else:
187+
query = query % tuple([db.literal(item) for item in args])
184188
try:
185189
r = None
186190
r = self._query(query)
@@ -236,7 +240,13 @@ def executemany(self, query, args):
236240
e = m.end(1)
237241
qv = m.group(1)
238242
try:
239-
q = [ qv % db.literal(a) for a in args ]
243+
q = []
244+
for a in args:
245+
if isinstance(a, dict):
246+
q.append(qv % dict((key, db.literal(item))
247+
for key, item in a.iteritems()))
248+
else:
249+
q.append(qv % tuple([db.literal(item) for item in a]))
240250
except TypeError, msg:
241251
if msg.args[0] in ("not enough arguments for format string",
242252
"not all arguments converted"):

0 commit comments

Comments
 (0)