Python'int32'无法转换为MySQL类型

时间:2014-07-23 13:58:12

标签: python mysql

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

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)

我现在想要添加一行,如下所示:

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

但是,当我运行它时,我收到以下错误消息:

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

我的语法是否有错误?

2 个答案:

答案 0 :(得分:0)

这可能是由于mysql的一个sh ** ty驱动程序。我非常自以为是的解决方案更新到python 3并使用mysqlclient pip install mysqlclient,问题将解决!瞧!

答案 1 :(得分:0)

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

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