gpt4 book ai didi

python - 为什么 INSERT INTO 失败并显示 'Operand should contain 1 column(s)' ?

转载 作者:行者123 更新时间:2023-12-01 05:00:27 24 4
gpt4 key购买 nike

我知道实际上有几十个关于同一错误的问题,并且我已经检查了所有这些问题。其中大多数与某人滥用 SELECT 语句有关,但我找不到任何与我的问题类似的问题。

conn = pymysql.connect(host='localhost',
port=3306,
user='root',
passwd='password',
db='nhl')
cur = conn.cursor()

#some code...

player = td.string.strip()
player = player.split(' (')
tID = teamList.index(team)
cur.execute("INSERT INTO players (Name,G,A,P,PlusMinus,PIM,S,H,BKS,GVA,TKA,TeamID)
VALUES (%s, %s, %s, %s, %s, %s, %s, %s, %s, %s, %s, %s)",
(player, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, tID))

pymysql.err.InternalError: (1241, '操作数应包含 1 列')

我真的不知道我哪里错了。除 NameVARCHAR 之外,所有数据库列均为 INT。这是使用 pymysql 在 Python 3.4 中编码的。

最佳答案

player = player.split(' (')

在此行之后,player 是一个字符串列表(而不是字符串),因此 cur.execute 不会让您将其插入为名称列。

查找这些错误的一个简单方法是尝试用文字替换这些值,例如:

cur.execute("INSERT INTO players (Name,G,A,P,PlusMinus,PIM,S,H,BKS,GVA,TKA,TeamID) 
VALUES (%s, %s, %s, %s, %s, %s, %s, %s, %s, %s, %s, %s)",
("playerName", 0,0,0,0,0,0,0,0,0,0, 123))

这会起作用,并且您会知道在其他地方查找错误。请参阅:How to debug by splitting the problem space.

关于python - 为什么 INSERT INTO 失败并显示 'Operand should contain 1 column(s)' ?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/26332998/

24 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com