python - MySql 连接器在 Python 中消失

标签 python mysql mysql-connector-python

编辑: 这个问题令人难以置信。我现在已经设法用 time.sleep(0.01) 替换了一个烦人的打印函数,但是到底为什么我应该从更慢的执行时间中受益,这超出了我的理解范围。

我在 Python 3.23 的 MySQL 1.0.7 连接器中迭代光标时遇到问题。

除非 print() 每次迭代的结果(这既愚蠢又耗时)我会引发以下错误:

raise errors.InterfaceError(errno=2013) mysql.connector.errors.InterfaceError: 2013: Lost connection to MySQL server during query

有什么想法吗?

到目前为止,代码很简单:

self.config = {'user': user,'password': password,'host': host,'database':     
database,'raise_on_warnings': True}

self.data = []
self.clickcnx = mysql.connector.connect(**self.config)
self.clickcursor = self.clickcnx.cursor()

query = "SELECT table1, table2, table3 FROM `db`-tables;"
self.clickcursor.execute(query)
for item in self.clickcursor:
  print(item)     #this is the strange line that I need!
  self.data.append(item)
self.clickcnx.close()

最佳答案

您错过了实际获取结果的部分,您只是跳过了光标。

试试这个版本:

query = "SELECT table1, table2, table3 FROM `db`-tables;"
self.clickcursor.execute(query)
results = self.clickcursor.fetchall()
for item in results:
  print(item)     #this is the strange line that I need!
  self.data.append(item)
self.clickcnx.close()

关于python - MySql 连接器在 Python 中消失,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/13045923/

相关文章:

python - 'str' 对象没有属性 'flatMap'

python - 将.dat文件中的数据读取到python中的不同变量中

Python 守护进程过早退出。其他一切也是如此

mysql - Jmeter 中的记录 Controller 未在本地主机 Web 服务器中记录

python - 在 For 循环中创建字典时防止覆盖嵌套字典值

MYSQL获取所有结果但首先

Python SQL - 两个左连接

python - mysql.connector.errors.ProgrammingError : 1083 (42000): Field separator argument is not what is expected

python - 创建或编辑模型实例时 Django admin 404 错误

MySQL自增主键增加10