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

我真的不确定我要去哪里错了。所有数据库列都是INT除了Namewhich is VARCHAR。这是使用 pymysql 在 Python 3.4 中编码的。

4

1 回答 1

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

which would work, and you'd know to look for the error elsewhere. See: How to debug by splitting the problem space.

于 2014-10-13T13:51:53.033 回答