3

In Python-MySQL, I have defined a table as follows:

TABLES['clusters'] = (
    "CREATE TABLE `clusters` ("
    "  `pid` int(8) NOT NULL, "
    "  `cluster` int(8), "
    "  `cluster_round` varchar(32), "
    "  PRIMARY KEY (`pid`), "
    "  FOREIGN KEY (`pid`) REFERENCES `persons` (`id`) "
    ") ENGINE=InnoDB")

for name, ddl in TABLES.iteritems():
    self.cursor.execute(ddl)

I now want to add a row as follows:

def set_clustering(self, clustering_dict, cluster_round):
    query = (
        "INSERT INTO clustering " 
        "(pid, cluster, cluster_round) "
        "VALUES (%s, %s, %s) " 
        "ON DUPLICATE KEY UPDATE cluster = cluster")

    for (pid, cluster) in clustering_dict.iteritems():
        self.cursor.execute(query, (pid, cluster, cluster_round))
    self.cnx.commit()

However, when I run this, I get the following error message:

mysql.connector.errors.ProgrammingError: 
Failed processing format-parameters; 
Python 'int32' cannot be converted to a MySQL type

Is there an error in my syntax?

3
  • Just a question about storing your schema in a dict. If you have references between tables (as you do), how do you make sure that persons exists before you create clusters? Commented Jul 23, 2014 at 14:01
  • I don't have an answer to that, I am also still looking for that. As a workaround, I just create the tables twice. Commented Jul 23, 2014 at 14:03
  • You could try a collections.OrderedDict or just a list. Commented Jul 23, 2014 at 15:02

2 Answers 2

2

i had a similar problem. its probably because you're trying to insert numpy int32 and mysql cant support it. you can convert a numpy int to python int by running numpyint.item().

i would recommend checking the type of each variable you're trying to insert (type(foo)) to make sure all the values are of type that mysql can support.

Sign up to request clarification or add additional context in comments.

Comments

1

This is likely due to one of the sh**ty drivers for mysql. My very opinionated solution. Update to python 3 and use mysqlclient pip install mysqlclient and the problem will be solved! Voila!

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.