简体   繁体   English

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

[英]Issue inserting into MySQL database in python

Currently receiving the following error when I try and insert into my database with python. 当我尝试使用python插入数据库时​​,当前收到以下错误。 If you all could help me out that would be greatly appreciated. 如果大家能帮助我,将不胜感激。

        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

Here is the code running: 这是运行的代码:

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)

Thanks in advance for the help! 先谢谢您的帮助!

I'm guessing INSET should be INSERT ? 我猜INSET应该是INSERT吗? Also you have 9 %s items in the query but 10 values (and 10 columns in the DB it seems). 另外,查询中有9 %s项目,但有10个值(看来数据库中有10列)。

Looking at your SQL error, I noticed as I broke it down: 在查看您的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);"

is quite a long line. 很长的线。 If you change the above line to 如果将以上行更改为

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 ;

Remove said ; 删除说; and change the line to this: 并将行更改为此:

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

Then your SQL should work. 然后您的SQL应该工作了。

I would also recommend instead of assigning w = win and so on, just have 我还建议不要分配w = win等,而只需

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

because you are just adding extra lines. 因为您只是添加额外的行。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM