我是靠谱客的博主 真实大米,最近开发中收集的这篇文章主要介绍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.

最后

以上就是真实大米为你收集整理的Mysql中循环拼接参数,MySQL在Python中;连接在第一个循环后在for循环中关闭的全部内容,希望文章能够帮你解决Mysql中循环拼接参数,MySQL在Python中;连接在第一个循环后在for循环中关闭所遇到的程序开发问题。

如果觉得靠谱客网站的内容还不错,欢迎将靠谱客网站推荐给程序员好友。

本图文内容来源于网友提供,作为学习参考使用,或来自网络收集整理,版权属于原作者所有。
点赞(58)

评论列表共有 0 条评论

立即
投稿
返回
顶部