Warning: file_get_contents(/data/phpspider/zhask/data//catemap/2/python/338.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
Python';int32';无法转换为MySQL类型_Python_Mysql - Fatal编程技术网

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

Python';int32';无法转换为MySQL类型,python,mysql,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 `perso

在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()
我现在想添加一行,如下所示:

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

我的语法有错误吗?

这可能是由于mysql的一个sh**ty驱动程序造成的。我非常固执己见的解决方案。更新到python 3并使用mysqlclient
pip安装mysqlclient
,问题就会解决!瞧

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


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

只是一个关于在dict中存储模式的问题。如果表之间有引用(就像您所做的那样),在创建
集群
之前,如何确保
人员
存在?我没有答案,我还在寻找答案。作为一种解决方法,我只创建了两次表。您可以尝试使用
collections.orderedict
或列表。