尝试从 MySQL 查询时出现 AttributeError: __enter__ 错误

Nir*_*Nir 4 python mysql mysql-connector python-3.x

以下内容曾经对我有用。由于某种原因,现在不再是这样了。

>>> 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__
>>>
Run Code Online (Sandbox Code Playgroud)

mysql_connector-2.2.9-cp37-cp37m-linux_x86_64.whl

Python 3.7.6(默认,2020 年 2 月 26 日,20:54:15)

Nir*_*Nir 10

解决方案:

pip uninstall mysql-connector-python
pip uninstall mysql-connector
pip install mysql-connector-python
Run Code Online (Sandbox Code Playgroud)

据我所知,问题是我在(新的新版本)mysql-connector之后安装了(已弃用)。千,成为python 可以使用的唯一库。mysql-connector-pythonmysql-connectormysql-connector-python