From 87d1145c0d6ee4f5a8ecf6d5c62d2479b9cf27ea Mon Sep 17 00:00:00 2001 From: Guillaume Bandet Date: Mon, 4 Nov 2013 16:17:57 +0100 Subject: Fix the conversion of list or tuple args to a SQL. When there is one element on the list, the generated SQL was (1,) (python notation of a single element tuple, which is not valid in SQL. --- MySQLdb/converters.py | 7 +++++-- MySQLdb/cursors.py | 5 ++++- 2 files changed, 9 insertions(+), 3 deletions(-) diff --git a/MySQLdb/converters.py b/MySQLdb/converters.py index 491d49b..26c1f90 100644 --- a/MySQLdb/converters.py +++ b/MySQLdb/converters.py @@ -129,13 +129,16 @@ def char_array(s): def array2Str(o, d): return Thing2Literal(o.tostring(), d) +def quote_tuple(t, d): + return "(%s)" % (','.join(escape_sequence(t, d))) + conversions = { IntType: Thing2Str, LongType: Long2Int, FloatType: Float2Str, NoneType: None2NULL, - TupleType: escape_sequence, - ListType: escape_sequence, + TupleType: quote_tuple, + ListType: quote_tuple, DictType: escape_dict, InstanceType: Instance2Str, ArrayType: array2Str, diff --git a/MySQLdb/cursors.py b/MySQLdb/cursors.py index 7e5a887..8815b80 100644 --- a/MySQLdb/cursors.py +++ b/MySQLdb/cursors.py @@ -180,7 +180,10 @@ class BaseCursor(object): if isinstance(query, unicode): query = query.encode(db.unicode_literal.charset) if args is not None: - query = query % db.literal(args) + if isinstance(args, dict): + query = query % {key: db.literal(item) for key, item in args.iteritems()} + else: + query = query % tuple([db.literal(item) for item in args]) try: r = None r = self._query(query) -- cgit v1.2.1 From 8bef3359fb27b63cf608a6685a99ed77fa766e5a Mon Sep 17 00:00:00 2001 From: Guillaume Bandet Date: Tue, 5 Nov 2013 11:01:22 +0100 Subject: Syntax fix for python 2.5 support. --- MySQLdb/cursors.py | 3 ++- 1 file changed, 2 insertions(+), 1 deletion(-) diff --git a/MySQLdb/cursors.py b/MySQLdb/cursors.py index 8815b80..9797d4a 100644 --- a/MySQLdb/cursors.py +++ b/MySQLdb/cursors.py @@ -181,7 +181,8 @@ class BaseCursor(object): query = query.encode(db.unicode_literal.charset) if args is not None: if isinstance(args, dict): - query = query % {key: db.literal(item) for key, item in args.iteritems()} + query = query % dict((key, db.literal(item)) + for key, item in args.iteritems()) else: query = query % tuple([db.literal(item) for item in args]) try: -- cgit v1.2.1 From 8096d8c0530b6c5ccec671c4746d0f94b4831790 Mon Sep 17 00:00:00 2001 From: Guillaume Bandet Date: Tue, 5 Nov 2013 11:34:43 +0100 Subject: Also fix executemany(). --- MySQLdb/cursors.py | 8 +++++++- 1 file changed, 7 insertions(+), 1 deletion(-) diff --git a/MySQLdb/cursors.py b/MySQLdb/cursors.py index 9797d4a..7c01870 100644 --- a/MySQLdb/cursors.py +++ b/MySQLdb/cursors.py @@ -240,7 +240,13 @@ class BaseCursor(object): e = m.end(1) qv = m.group(1) try: - q = [ qv % db.literal(a) for a in args ] + q = [] + for a in args: + if isinstance(a, dict): + q.append(qv % dict((key, db.literal(item)) + for key, item in a.iteritems())) + else: + q.append(qv % tuple([db.literal(item) for item in a])) except TypeError, msg: if msg.args[0] in ("not enough arguments for format string", "not all arguments converted"): -- cgit v1.2.1