概述
My MySQL connection closes after one loop cycle. I probably do something wrong in the usage of the mysql object. It outputs one cycle and then exits with an error.
This is my code:
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)
The error I get is:
Traceback (most recent call last):
File "trade.py", line 47, in
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.
解决方案
MySQL doesn't support real cursors, and the library only emulates them. As such, you can only run one cursor per connection at a time.
In the loop you are opening a second cursor, and that's not something MySQL can handle. Read all rows from the first query before using the cursor for a new query.
最后
以上就是真实大米为你收集整理的Mysql中循环拼接参数,MySQL在Python中;连接在第一个循环后在for循环中关闭的全部内容,希望文章能够帮你解决Mysql中循环拼接参数,MySQL在Python中;连接在第一个循环后在for循环中关闭所遇到的程序开发问题。
如果觉得靠谱客网站的内容还不错,欢迎将靠谱客网站推荐给程序员好友。
发表评论 取消回复