在python中插入MySQL数据库时出现问题

时间:2018-08-08 12:18:55

标签: python mysql

当我尝试使用python插入数据库时​​,当前收到以下错误。如果大家能帮助我,将不胜感激。

        league_Build_Data(1,0,'Infinity Edge, Mobility Boots, Black Clever, Deaths Dance, Essense Reaver, Guardian Angle', 2431, 'Caitlyn, Blitzcrank, Lee Sin, Ahri, Trundle', 'Anivia, Akali, Draven, Nami, Fiddle Sticks', 12, 3, 15, 231)

mysql.connector.errors.ProgrammingError: 1064 (42000): 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 'kill, death, assist, creep_score) VALUES (1, 0, 'Infinity Edge, Mobility Boots, ' at line 1

这是正在运行的代码:

import mysql.connector



def league_Build_Data(win, lose, build, game_Length, champions_team, champions_enemy, kill, death, assist, creep_score):
    sql = mysql.connector.connect(host="localhost",user="user",passwd="pass", database="league_Data")
    print("Connected to database")
    cursor = sql.cursor()
    query = ("INSERT INTO league_Build_Data"
        "(win, lose, build, game_Length, champions_team, champions_enemy, kill, death, assist, creep_score)"
        "VALUES (%s, %s, %s, %s, %s, %s, %s, %s, %s, %s)")
    value = (win, lose, build, game_Length, champions_team, champions_enemy, kill, death, assist, creep_score)
    cursor.execute(query, value)
    sql.commit();
    print(cursor.rowcount, "Leauge of Legends build data recorded into database!")


#Testing function
win = 1
lose = 0
build = 'Infinity Edge, Mobility Boots, Black Clever, Deaths Dance, Essense Reaver, Guardian Angle'
game_Length = 2431
champions_team = 'Caitlyn, Blitzcrank, Lee Sin, Ahri, Trundle'
champions_enemy = 'Anivia, Akali, Draven, Nami, Fiddle Sticks'
kill = 12
death = 3
assist = 15
creep_score = 231
league_Build_Data(win, lose, build, game_Length, champions_team, champions_enemy, kill, death, assist, creep_score)

预先感谢您的帮助!

2 个答案:

答案 0 :(得分:2)

我猜INSET应该是INSERT吗?另外,您在查询中有9个%s项目,但有10个值(看来数据库中有10列)。

答案 1 :(得分:1)

在查看您的SQL错误时,我发现它已分解:

query = "INSERT INTO league_Build_Data (win, lose, build, game_Length, champions_team, champions_enemy, kill, death, assist, creep_score) VALUES (%s, %s, %s, %s, %s, %s, %s, %s, %s, %s);"

很长。 如果将以上行更改为

query = ("INSERT INTO league_Build_Data "
        "(win, lose, build, game_Length, champions_team, champions_enemy, kill, death, assist, creep_score) "
        "VALUES (%s, %s, %s, %s, %s, %s, %s, %s, %s, %s); ") #<--- Notice the ;

删除所说的; 并将行更改为此:

query = ("INSERT INTO league_Build_Data"
        "(win, lose, build, game_Length, champions_team, champions_enemy, kill, death, assist, creep_score)"
        "VALUES (%s, %s, %s, %s, %s, %s, %s, %s, %s, %s)")

然后您的SQL应该可以工作。

我也建议不要分配w = win,以此类推,

value = (win, lose, build, game_Length, champions_team....)

因为您只是添加了额外的行。