python - 生成的查询上的mysql语法错误

时间:2013-03-04 22:50:40

标签: python mysql sql

直到最近,以下代码才能正常运行。

for player in away_starters_names:
    on = 0
    this_player = player[0]
    if this_player in event:
        player_id = away_team_dict[this_player]
        player_id = int(player_id[0])
        team = 0
        team_id = away_id
        cur.execute("""INSERT INTO football.match_subs(player, time, added, game_id, home, on, team_id) VALUES (%s, %s, %s, %s, %s, %s, %s) ON DUPLICATE KEY UPDATE game_id = game_id""", (player_id, time, added, game_id, team, on, team_id))
        db.commit()

现在出现此错误:

    Traceback (most recent call last):
  File "Z:\Coding\bbc\find_teams.py", line 508, in <module>
    cur.execute("""INSERT INTO football.match_subs(player, time, added, game_id, home, on, team_id) VALUES (%s, %s, %s, %s, %s, %s, %s) ON DUPLICATE KEY UPDATE game_id = game_id""", (player_id, time, added, game_id, team, on, team_id))
  File "C:\Python27\lib\site-packages\MySQLdb\cursors.py", line 202, in execute
    self.errorhandler(self, exc, value)
  File "C:\Python27\lib\site-packages\MySQLdb\connections.py", line 36, in defaulterrorhandler
    raise errorclass, errorvalue
ProgrammingError: (1064, "You have an error in your SQL syntax; check the manual that corresponds to your MySQL server version for the right syntax to use near 'on, team_id) VALUES (299191, 89.33, 0, 21570967, 0, 0, 2) ON DUPLICATE KEY UPDAT' at line 1")

有什么问题?

1 个答案:

答案 0 :(得分:2)

在字段名称周围使用反引号。 ON是MySQL中的保留字