Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

MySql connector dies in Python

EDIT: This problem is unbelievable. I have now managed to replace an annoying print function with the time.sleep(0.01), but why on earth I should benefit from a SLOWER execution time is beyond me.

I have a problem in iterating over my cursor in MySQL 1.0.7 connector for Python 3.23.

Unless print() the result of each iteration (which is both silly and time consuming) I get the following error raised:

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

Any thoughts?

the code is trivial 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()
like image 968
user1597652 Avatar asked Oct 24 '12 08:10

user1597652


People also ask

Does MySQL support Python 3?

MySQLdb module, a popular interface with MySQL is not compatible with Python 3. Instead, we shall use PyMySQL module.


2 Answers

You are missing the part where you actually fetch the results, you are just stepping over the cursor.

Try this version:

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()
like image 79
Burhan Khalid Avatar answered Oct 03 '22 00:10

Burhan Khalid


I had the same issue with 1.0.7. Adding a time.sleep() fixed it. Then I upgraded to 1.0.10 and removed the sleep. That also works fine.

So the solution seems to be:

pip install --upgrade mysql-connector-python

You might need to use sudo.

like image 31
Daniël van Eeden Avatar answered Oct 03 '22 02:10

Daniël van Eeden