I'm currently using PyMySQL to make queries to my MySQL DB. I wanted the results to be a dictionary, where the columns are the keys and with their associated values, and I've got that. But I've noticed that values for types like dates and decimals are returned as objects, which is not what I've encountered with other languages and libraries. An example is:
{'date': datetime.date(2023, 12, 26), 'store_number': 7, 'total': Decimal('11336.43')}
Ideally, I just want something like:
{'date': '2023-12-26', 'store_number': 7, 'total': 10036.43}
What would be the best way to do this? I don't necessarily need to use PyMySQL, but it seems like a popular choice. I've read and even asked ChatGPT, but I figured I'd ask real people about what they have done and know. The options I've found are:
- Some suggest to iterate or use list comprehensions to convert the values, but that seems a little strange, maybe even silly, to me that I need to do that or write a custom function to do this every time.
- Using converters. This one seemed more reasonable.
- Casting values in the query. This also seems odd to me, as I've never had to resort to doing something like this before.
Any help or insight would be appreciated. Thanks!