我有一个将数据写入MySQL的python程序,由于某种未知原因,当我调试该程序时,它总是第一次无法建立连接,而无需进行任何更改,它第二次成功建立了连接
这是相关代码:
try:
......
connection = mysql.connector.connect(host='localhost',
database='mydb',
user='theuser',
password='thepassword')
......
for case in cases:
if len(case) > 0:
......
if (connection.is_connected()):
c1 = connection.cursor()
c1.execute(query, args)
connection.commit()
print("New case inserted.")
except mysql.connector.Error as error:
print("Error: {}".format(error))
finally:
if (connection.is_connected()):
connection.close()
print("MySQL connection is closed")
第一次运行失败,如下所示:
错误:
错误:2003:无法连接到“ localhost:3306”上的MySQL服务器(10060 连接尝试失败,因为被连接方未 一段时间后正确响应或建立连接 失败,因为连接的主机无法响应)
第二次运行将成功。
非常感谢您。