Python / mysql.connector-在多个游标之间重用单个数据库连接

时间:2018-08-02 19:04:12

标签: python mysql mysql-connector

我正在尝试找到一种在多个执行之间重用数据库连接的方法。但是在执行第二个“ db.cursor()”时,我得到“ mysql.connector.errors.OperationalError:MySQL连接不可用。”

import mysql.connector

db = mysql.connector.connect(host="***", user="***", password="***", database="***")
db.autocommit = True

cursor = db.cursor()
cursor.execute("call procedure1();")
rows1 = cursor.fetchall()

cursor = db.cursor()
cursor.execute("call procedure2()")
rows2 = cursor.fetchall()

1 个答案:

答案 0 :(得分:0)

您必须打开多个连接。 MySQL是线程安全的,您可以让两个线程共享同一连接,多个线程无法在同一连接上同时向MySQL服务器发送查询,因此每个连接将能够访问其各自的游标,查询和结果设置而不会影响其他连接,但是每个线程或进程将需要自己的连接。