Python';int32';无法转换为MySQL类型
在Python 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
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
或列表。