MySql连接器在Python中死亡

2024-06-16 10:21:48 发布

您现在位置:Python中文网/ 问答频道 /正文

编辑: 这个问题令人难以置信。我现在已经设法用时间。睡觉(0.01),但我不明白为什么我应该从较慢的执行时间中获益。在

在MySQL 1.0.7 connector for python3.23中迭代游标时出现问题。在

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

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

有什么想法吗?在

代码是微不足道的thusfar:

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()

Tags: selfconfigforconnector时间mysqlitemquery
3条回答

我遇到了MySQL连接器的问题。我试过伯翰·哈立德的答案,但没用。我可以通过添加groupby语句来解决我的问题。在

原始查询:

SELECT
    a.ID,
    a.UID,
    g.GroupId,
FROM Accounts a
    LEFT JOIN Profile p ON p.ID = a.ID
    LEFT JOIN Group g ON g.GroupId = p.GroupId
WHERE
    a.UID IS NOT NULL
    AND a.Active = 1
    AND a.Type = 1;

返回与原始结果相同的查询:

^{pr2}$

唯一的区别是第二个查询没有抛出InterfaceError(2013)。我的查询返回大约250000行。我对每一个查询都运行了一个解释,令我惊讶的是,第二个查询比第一个查询花费的时间更长(持续时间),因为它将结果存储在一个临时表中,但是获取时间从6.00s变为0.50s!在

尝试添加一个由agregator组成的组,看看这是否解决了您的问题。当然,你需要确保你的结果是一样的。我建议对一个表值执行groupby,您知道这个值将是唯一的。在

您缺少实际获取结果的部分,您只是在光标上跨出一步。在

试用此版本:

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()

我对1.0.7也有同样的问题。添加时间。睡觉()修好了。然后我升级到1.0.10并取消了休眠。这也很好。在

所以解决办法似乎是:

pip install  upgrade mysql-connector-python

你可能需要使用sudo。在

相关问题 更多 >