I am facing a problem using python, mysql.connector (8.1.0) and trying to open 2 connections on 2 different servers:
If I run :
from mysql.connector import MySQLConnectionif __name__ in '__main__': # A try: c1 = MySQLConnection( host='host1', user='*', password='*', database='A' ) c1.close() except Exception as e: print(e) finally: print('c1') # B try: c2 = MySQLConnection( host='host2', user='*', password='*', database='B' ) c2.close() except Exception as e: print(e) finally: print('c2')
I got exception : Character set 'utf8' unsupported
for c2
If I run only part B, it's Ok. It's as if something was set globally after the first connection.
any idea?
EDIT: Got it ! CharacterSet.desc
is a class variable set at begining.
from mysql.connector import MySQLConnection as MySQLConnectionfrom mysql.connector.constants import CharacterSetif __name__ in '__main__': desc = CharacterSet.desc.copy() try: c1 = MySQLConnection( host='host1', user='*', password='*', database='A' ) c1.close() except Exception as e: print(e) finally: print('c1') CharacterSet.desc = desc try: c2 = MySQLConnection( host='host2', user='*', password='*', database='B' ) c2.close() except Exception as e: print(e) finally: print('c2')
It works now