Mysql中循环拼接参数,MySQL在Python中;连接在第一个循环后在for循环中关闭

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.

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值