我的 MySQL 连接在一个循环周期后关闭。我可能在使用 mysql 对象时做错了什么。它输出一个周期,然后以错误退出。
这是我的代码:
cnx = mysql.connector.connect(**config)
cursor = cnx.cursor()
fetch_ids = cnx.cursor()
query = ("SELECT * FROM items WHERE has_recipe = 1")
fetch_ids.execute(query)
count = 0
for (data) in fetch_ids:
fetch_details = cnx.cursor()
query = ("SELECT * FROM recipes WHERE recipe_id = " + str(data[1]))
fetch_details.execute(query)
我得到的错误是:
Traceback (most recent call last):
File "trade.py", line 47, in <module>
fetch_details = cnx.cursor()
File "/Users/allendar/Desktop/mysql/connector/connection.py", line 1076, in cursor
raise errors.OperationalError("MySQL Connection not available.")
mysql.connector.errors.OperationalError: MySQL Connection not available.