在python中插入mysql数据库的问题

wvmv3b1j  于 2021-06-23  发布在  Mysql
关注(0)|答案(2)|浏览(453)

当我尝试用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)

提前感谢您的帮助!

ajsxfq5m

ajsxfq5m1#

我猜是的 INSET 应该是 INSERT ? 你还有9个 %s 查询中的项,但有10个值(db中似乎有10列)。

mhd8tkvw

mhd8tkvw2#

查看您的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....)

因为你只是在添加额外的行。

相关问题