Skip to content

Commit f48a294

Browse files
authored
Fix dict parameter support (PyMySQL#324)
1 parent bf0ef58 commit f48a294

File tree

2 files changed

+18
-1
lines changed

2 files changed

+18
-1
lines changed

MySQLdb/cursors.py

+6-1
Original file line numberDiff line numberDiff line change
@@ -181,7 +181,12 @@ def execute(self, query, args=None):
181181

182182
if args is not None:
183183
if isinstance(args, dict):
184-
args = dict((key, db.literal(item)) for key, item in args.items())
184+
nargs = {}
185+
for key, item in args.items():
186+
if isinstance(key, unicode):
187+
key = key.encode(db.encoding)
188+
nargs[key] = db.literal(item)
189+
args = nargs
185190
else:
186191
args = tuple(map(db.literal, args))
187192
try:

tests/test_cursor.py

+12
Original file line numberDiff line numberDiff line change
@@ -1,3 +1,5 @@
1+
from __future__ import print_function, absolute_import
2+
13
import pytest
24
import MySQLdb.cursors
35
from configdb import connection_factory
@@ -76,3 +78,13 @@ def test_executemany():
7678
assert cursor._executed.endswith(b"(3, 4),(5, 6)"), "executemany with %% not in one query"
7779
finally:
7880
cursor.execute("DROP TABLE IF EXISTS percent_test")
81+
82+
83+
def test_pyparam():
84+
conn = connect()
85+
cursor = conn.cursor()
86+
87+
cursor.execute(u"SELECT %(a)s, %(b)s", {u'a': 1, u'b': 2})
88+
assert cursor._executed == b"SELECT 1, 2"
89+
cursor.execute(b"SELECT %(a)s, %(b)s", {b'a': 3, b'b': 4})
90+
assert cursor._executed == b"SELECT 3, 4"

0 commit comments

Comments
 (0)