I am new to Python (I mean really new > 7 weeks in on my free time) , bear with me...
I am trying to get exchange info from Binance for a project.
I am using the python-binance package.I get the exchange info with:
exch_info = client.get_exchange_info()
The result is a very complicated mix from which I want to extract 'baseAsset' and 'quoteAsset' and put it in a table I already have based on my field 'Ticker' ('symbol' in the response).
see https://python-binance.readthedocs.io/en/latest/binance.html#binance.client.Client.get_exchange_info
I extract the list of dictionnaries I want by adding ['symbols']:
exch_info = client.get_exchange_info()['symbols']
I have tried (don't judge me) but I am stuck. I want to end up with a MySQL query that currently looks like:
exch_info = client.get_exchange_info()['symbols']
mycursor.executemany(f"INSERT INTO `param_asset_binance` (Ticker,Coin1,Coin2) VALUES ( %(symbol)s, %(baseAsset)s, %(quoteAsset)s) ON DUPLICATE KEY UPDATE Coin1 = VALUES(Coin1), Coin2 = VALUES(Coin2) ", exch_info)
#mydb.commit()
My errors are:
Traceback (most recent call last):
File "C:\Users\mathi\anaconda3\lib\site-packages\mysql\connector\conversion.py", line 179, in to_mysql
return getattr(self, "_{0}_to_mysql".format(type_name))(value)
AttributeError: 'MySQLConverter' object has no attribute '_list_to_mysql'
During handling of the above exception, another exception occurred:
Traceback (most recent call last):
File "C:\Users\xxxx\anaconda3\lib\site-packages\mysql\connector\cursor.py", line 395, in _process_params_dict
conv = to_mysql(conv)
File "C:\Users\xxxx\anaconda3\lib\site-packages\mysql\connector\conversion.py", line 181, in to_mysql
raise TypeError("Python '{0}' cannot be converted to a "
TypeError: Python 'list' cannot be converted to a MySQL type
During handling of the above exception, another exception occurred:
Traceback (most recent call last):
File "c:\Users\xxxx\library_param.py", line 45, in <module>
update_binance_exch_info()
File "c:\Users\xxxx\library_param.py", line 42, in update_binance_exch_info
mycursor.executemany(f"INSERT INTO `autobot_core`.`param_asset_binance` (Ticker,Coin1,Coin2) VALUES ( %(symbol)s, %(baseAsset)s, %(quoteAsset)s) ON DUPLICATE KEY UPDATE Coin1 = VALUES(Coin1), Coin2 = VALUES(Coin2) ", exch_info)
File "C:\Users\xxxx\anaconda3\lib\site-packages\mysql\connector\cursor.py", line 652, in executemany
stmt = self._batch_insert(operation, seq_params)
File "C:\Users\xxxx\anaconda3\lib\site-packages\mysql\connector\cursor.py", line 591, in _batch_insert
tmp, self._process_params_dict(params))
File "C:\Users\xxxx\anaconda3\lib\site-packages\mysql\connector\cursor.py", line 403, in _process_params_dict
raise errors.ProgrammingError(
mysql.connector.errors.ProgrammingError: Failed processing pyformat-parameters; Python 'list' cannot be converted to a MySQL type
what am I doing wrong?
The issue is related to the exch_info
parameter provided in the execute_many
. As it is unable to convert the type list into MYSQL format.
params = []
for row in exch_info:
params.append({
'symbol': row['symbol'],
'baseAsset': row['baseAsset'],
'quoteAsset': row['quoteAsset']
})
mycursor.executemany(f"INSERT INTO `param_asset_binance` (Ticker,Coin1,Coin2) VALUES ( %(symbol)s, %(baseAsset)s, %(quoteAsset)s) ON DUPLICATE KEY UPDATE Coin1 = VALUES(Coin1), Coin2 = VALUES(Coin2) ", params)
Reference :
How can I use executemany to insert into MySQL a list of dictionaries in Python
https://dev.mysql.com/doc/connector-python/en/connector-python-api-mysqlcursor-executemany.html