Merge pull request #3656 from UtahDave/fix_mysql

Fixes #3653 Stop MySQLdb from converting to python objects.
This commit is contained in:
Thomas S Hatch 2013-02-08 14:59:39 -08:00
commit 0bcc2a491b

View File

@ -28,6 +28,7 @@ import sys
try: try:
import MySQLdb import MySQLdb
import MySQLdb.cursors import MySQLdb.cursors
import MySQLdb.converters
HAS_MYSQLDB = True HAS_MYSQLDB = True
except ImportError: except ImportError:
HAS_MYSQLDB = False HAS_MYSQLDB = False
@ -106,6 +107,7 @@ def connect(**kwargs):
_connarg('pass', 'passwd') _connarg('pass', 'passwd')
_connarg('port') _connarg('port')
_connarg('db') _connarg('db')
_connarg('conv')
_connarg('unix_socket') _connarg('unix_socket')
_connarg('default_file', 'read_default_file') _connarg('default_file', 'read_default_file')
@ -150,8 +152,15 @@ def query(database, query):
# Doesn't do anything about sql warnings, e.g. empty values on an insert. # Doesn't do anything about sql warnings, e.g. empty values on an insert.
# I don't think it handles multiple queries at once, so adding "commit" # I don't think it handles multiple queries at once, so adding "commit"
# might not work. # might not work.
# The following 3 lines stops MySQLdb from converting the MySQL results
# into Python objects. It leaves them as strings.
orig_conv = MySQLdb.converters.conversions
conv_iter = iter(orig_conv)
conv = dict(zip(conv_iter,[str,] * len(orig_conv.keys())))
ret = {} ret = {}
dbc = connect(**{'db': database}) dbc = connect(**{'db': database, 'conv': conv})
cur = dbc.cursor() cur = dbc.cursor()
start = time.time() start = time.time()
affected = cur.execute(query) affected = cur.execute(query)