Postgres错误“错误:INSERT的目标列多于表达式”

时间:2017-12-04 17:19:24

标签: python postgresql

每当我运行此代码时,我都会收到错误消息"错误%s%e INSERT的目标列多于表达式"在fthg下显示^。知道问题在哪里,考虑到fthg已经定义了吗?非常感谢!

 cur.execute("CREATE TABLE IF NOT EXISTS soccerleague.games (gameId SERIAL PRIMARY KEY, homeTeamId INTEGER, FOREIGN KEY (homeTeamId) REFERENCES soccerleague.teams(id),awayTeamId INTEGER, FOREIGN KEY (awayTeamId) REFERENCES soccerleague.teams(id), fthg INTEGER, athg INTEGER, ftr VARCHAR, refereeId INTEGER, FOREIGN KEY (refereeId) REFERENCES soccerleague.referees(id), HY INTEGER, AY INTEGER)")

data3 = (teamsDict[homeTeam], teamsDict[awayTeam], fthg, athg, ftr, refereesDict[refereeName], hy, ay)
query3 = "INSERT INTO soccerleague.games (homeTeamId, awayTeamId, fthg, athg, ftr, refereeId, hy, ay) VALUES"
query3 += "('" + str(data3) + "', '" + x + "'),"
query3 = query3[:-1] + ";"

1 个答案:

答案 0 :(得分:0)

问题不在表创建中,而是在设置要插入的数据时。

query3 += "('" + str(data3) + "', '" + x + "'),"行指示首先插入data3的内容,然后将x的内容加到2个参数中。 insert语句query3 = "INSERT INTO soccerleague.games (homeTeamId, awayTeamId, fthg, athg, ftr, refereeId, hy, ay) VALUES"引用了8个参数。要解决此问题,请提供8个值(某些值可能为null)或仅将insert语句减少到2个第一个字段。

即使您将data3设置为包含所需的所有值,insert语句也会将其写为单引号字符串,该字符串仅映射到第1个字段。