2

Currently each element in the the object obtained after a query is stored as the following:

((1L, Decimal('30'), Decimal('17')), (2L, Decimal('29'), Decimal('16')))

I want it in the format:

(("1","30","17"),("2","29","16"))

This is because I have to later create a dict with this using zip(), then pass it on as a json.

Now of course I can iterate over the entire result, go into each element and str() it, but then with huge result sets it would be a very big overhead.

Is there a faster, simpler way to achieve this?

alecxe
  • 462,703
  • 120
  • 1,088
  • 1,195
user1265125
  • 2,608
  • 8
  • 42
  • 65

1 Answers1

2

You need to override the conversions mapping by passing conv argument to the MySQLdb.connect():

import MySQLdb
from MySQLdb.constants import FIELD_TYPE

conv = MySQLdb.converters.conversions.copy()
conv[FIELD_TYPE.DECIMAL] = conv[FIELD_TYPE.NEWDECIMAL] = str

db = MySQLdb.connect(host='...', user='...', passwd='...', db='...', conv=conv)

Now, all of the Decimal values would be returned as strings.

Also see relevant threads:

Community
  • 1
  • 1
alecxe
  • 462,703
  • 120
  • 1,088
  • 1,195