下面的方法曾经对我很有效。出于某种原因,它不再是了。
>>> import configparser
>>> from mysql.connector import connect, Error
>>> with open('file.cfg', 'r') as f:
... config_string = '[s]\n' + f.read()
...
>>> config = configparser.ConfigParser()
>>> config.read_string(config_string)
>>> with connect(host=config.get('s','HOST'),user=config.get('s','USER'),password=config.get('s','PASS'),database="db") as connection:
... with connection.cursor() as cursor:
... cursor.execute("select dt from tab limit 1")
...
Traceback (most recent call last):
File "<stdin>", line 1, in <module>
AttributeError: __enter__
>>>mysql_connector-2.2.9-cp37-cp37m-linux_x86_64.whl
Python 3.7.6 (默认,2020年2月26日,20:54:15)
发布于 2021-01-19 15:51:16
解决方案:
pip uninstall mysql-connector-python
pip uninstall mysql-connector
pip install mysql-connector-python我所看到的问题是,我在mysql-connector-python (新的mysql-connector版本)之后安装了mysql-connector (已被弃用)。这使得mysql-connector-python成为python唯一可以使用的库。
https://stackoverflow.com/questions/65787230
复制相似问题