Connector / python executemany NoneType


I have a little problem with Connector/python executing insert query.

I have function to insert data into database. Argument data is list of tuples: [(2652884, '', '', 1), ..... ]

def insert_url(self, data):

    query = "INSERT INTO `sp_urls` (`parent_id`, `url`, `version`, `hits`) VALUES (%d, %s, %s, %d) ON DUPLICATE KEY UPDATE url=url"        

        cursor = self.__cnx.cursor()
        cursor.executemany(query, data)
    except (mysql.connector.errors.IntegrityError) as err:
        print("Query syntax error:", err, file=sys.stderr)
    except (mysql.connector.errors.ProgrammingError) as err:
        print("Programming err:{0}".format(err))

The query itself works, in self.__cnx is initialized connection. Here's the traceback:

  File "sdi/database/", line 91, in process_records
  File "/home/david/workspace/stickydi/sdi/database/", line 87, in insert_url
    cursor.executemany(query, data)
  File "/usr/lib/python3/dist-packages/mysql/connector/", line 492, in executemany
    return self._batch_insert(operation,seq_params)
  File "/usr/lib/python3/dist-packages/mysql/connector/", line 428, in _batch_insert
    fmt =
AttributeError: 'NoneType' object has no attribute 'group'

I have one very similar method, and it works OK, I just can't see, why does executemany( ) goes wrong.

You should not use %d for SQL parameters. Stick to %s and let the MySQL connector handle the types:

query = """\
    INSERT INTO `sp_urls` (`parent_id`, `url`, `version`, `hits`)
    VALUES (%s, %s, %s, %s)

Quoting from the Python-MySQL documentation:

String constant stating the type of parameter marker formatting expected by the interface. Set to 'format' = ANSI C printf format codes, e.g. '...WHERE name=%s'. If a mapping object is used for conn.execute(), then the interface actually uses 'pyformat' = Python extended format codes, e.g. '...WHERE name=%(name)s'. However, the API does not presently allow the specification of more than one style in paramstyle.

Granted, using %s for SQL parameters is confusingly similar to Python string formatting, but it is not the same.