为什么 INSERT INTO 失败并显示"操作数应包含 1 列"?



我知道关于同一个错误有几十个问题,我已经全部检查过了。其中大多数都与有人滥用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, 'Operand should contain 1 column(s)')

我真的不确定哪里出了问题。除了NameVARCHAR之外,所有数据库列都是INT。这是在Python 3.4中使用pymysql编写的。

player = player.split(' (')

在这一行之后,player是字符串列表(而不是字符串),因此cur.execute不允许将其作为Name列的值进行插值。

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

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

这是可行的,你应该知道在其他地方查找错误。请参阅:如何通过拆分问题空间进行调试。

最新更新