python MySQL Connector

python mysql-connector pip
mysql.connector.connect python
mysql-connector-python github
python mysql connector binary
raspberry pi install python-mysql-connector
python mysql windows
python mysql query
python database

I'm playing with big data in Python and MySQL.

I've a got a huge table, I need to insert new rows while I'm fetching query's results.

I've got this error:

Traceback (most recent call last):
  File "recsys.py", line 53, in <module>
    write_cursor.executemany(add_sim, data_sims)
  File "/Library/Python/2.7/site-packages/mysql/connector/cursor.py", line 603, in executemany
    self._connection.handle_unread_result()
  File "/Library/Python/2.7/site-packages/mysql/connector/connection.py", line 1057, in handle_unread_result
    raise errors.InternalError("Unread result found")
mysql.connector.errors.InternalError: Unread result found

The code is the following one:

#!/usr/bin/env python
# -*- coding: utf-8 -*-

import logging
import mysql.connector

import numpy
import scipy
from scipy.spatial import distance

logging.basicConfig(filename='recsys.log', level=logging.INFO)

cnx = mysql.connector.connect(user='...', password='...', database='...')

cursor = cnx.cursor(dictionary=True)
write_cursor = cnx.cursor()


query = ("...")

cursor.execute(query)

while True:
    rows = cursor.fetchmany(100)
    if not rows:
        break

    add_sim = ("...")
    data_sims = []

    for row in rows:
        f1 = row['f1']
        f2 = row['f2']
        v1 = [[row['a1'], row['b1'], row['c1']]]
        v2 = [[row['a2'], row['b2'], row['c2']]]

        c = 1 - scipy.spatial.distance.cdist(v1, v2, 'cosine')

        if c > 0.8:

            data_sim = (f1, f2, c)
            data_sims.append(data_sim)

    write_cursor.executemany(add_sim, data_sims)
    cnx.commit()

cursor.close()

cnx.close()

I know I could use a buffered connection to mysql, but it's not a good choice in my case because of how really big my table is!

This is a documented behaviour of cursor.fetchmany() :

You must fetch all rows for the current query before executing new statements using the same connection.

To overcome this issue, you can establish a new connection to be used by write_cursor:

cnx_write = mysql.connector.connect(...)
write_cursor = cnx_write.cursor()

Install MySQL Driver. Python needs a MySQL driver to access the MySQL database. In this tutorial we will use the driver "MySQL Connector". We recommend  This manual describes how to install and configure MySQL Connector/Python, a self-contained Python driver for communicating with MySQL servers, and how to use it to develop database applications. MySQL Connector/Python 8.0 is highly recommended for use with MySQL Server 8.0, 5.7, and 5.6.

I had a similar problem when I was threading, and just had to close and open a new connection in each thread separately... instead of leaving the connection open.

Project description. MySQL driver written in Python which does not depend on MySQL C client libraries and implements the DB API v2.0 specification (PEP-249)​. MySQL Connector/Python is a standardized database driver for Python platforms and development. Additionally, MySQL Connector/Python 8.0 supports the new X DevAPI for development with MySQL Server 8.0.

Limit 100 in your query.

query = ("..."),  //limit 100 here

Then use fetchall() instead of fetchmany(100)

MySQL Connector/Python enables Python programs to access MySQL databases, using an API that is compliant with the Python Database API Specification v2.0 (  The pip command allows you to install MySQL Python connector on any Operating system including Windows, macOS, Linux, and Unix: pip install mysql-connector-python. If you found any issue while installing, you can explicitly specify the module version as follows: pip install mysql - connector - python==8.0.17.

MySQL Connector/Python allows you to compress the data stream between Python and MySQL database server using protocol compression. It supports  MySQL-Connector-Python module in Python MySQL is a Relational Database Management System (RDBMS) whereas the structured Query Language (SQL) is the language used for handling the RDBMS using commands i.e Creating, Inserting, Updating and Deleting the data from the databases.

Python MySQL tutorialUsing MySQL Connector Python mainly focuses on accessing the MySQL database, perform data insertion, data retrieval  MySQL driver written in Python. Project description. Project details. Release history. Project description. MySQL driver written in Python which does not depend on MySQL C client libraries and implements the DB API v2.0 specification (PEP-249). Project details. View statistics for this project via Libraries.io, or by using our public dataset on

Steps to connect MySQL database in Python using MySQL Connector Python. Install MySQL Connector Python using pip. Use the mysql. Connector/Python supports a C extension that interfaces with the MySQL C client library. For queries that return large result sets, using the C Extension can improve performance compared to a “ pure Python ” implementation of the MySQL client/server protocol.