Skip to content

Navigation Menu

Sign in
Appearance settings

Search code, repositories, users, issues, pull requests...

Provide feedback

We read every piece of feedback, and take your input very seriously.

Saved searches

Use saved searches to filter your results more quickly

Appearance settings

Commit 44f8b4d

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

File tree

Expand file treeCollapse file tree

2 files changed

+17
-4
lines changed
Filter options
Expand file treeCollapse file tree

2 files changed

+17
-4
lines changed

‎MySQLdb/converters.py

Copy file name to clipboardExpand all lines: MySQLdb/converters.py
+5-2Lines 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

Copy file name to clipboardExpand all lines: MySQLdb/cursors.py
+12-2Lines 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 (0)
Morty Proxy This is a proxified and sanitized view of the page, visit original site.