Python 'int32' 无法转换为 MySQL 类型 [英] Python 'int32' cannot be converted to a MySQL type

查看:83
本文介绍了Python 'int32' 无法转换为 MySQL 类型的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

在 Python-MySQL 中,我定义了一个表如下:

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

我的语法有错误吗?

推荐答案

我遇到了类似的问题.这可能是因为您试图插入 numpy int32 而 mysql 无法支持它.您可以通过运行 numpyint.item() 将 numpy int 转换为 python int.

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

我建议检查您尝试插入的每个变量的类型 (type(foo)),以确保所有值都是 mysql 可以支持的类型.

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.

这篇关于Python 'int32' 无法转换为 MySQL 类型的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆