获得;SQL语句中未使用所有参数;尝试使用python将数据导入MySQL时出错

获得;SQL语句中未使用所有参数;尝试使用python将数据导入MySQL时出错,python,mysql,Python,Mysql,我正试图从一个网站上抓取数据并上传到MySQL数据库中。但我一直在犯错误: “并非SQL语句中使用了所有参数” 代码如下: player_stats = [] table = soup.find('tbody') table_row = table.findAll('tr') for row in table_row: td = row.findAll('td') if td: player_dict = {} player_dict['POS']

我正试图从一个网站上抓取数据并上传到MySQL数据库中。但我一直在犯错误:

“并非SQL语句中使用了所有参数”

代码如下:

player_stats = []
table = soup.find('tbody')
table_row = table.findAll('tr')
for row in table_row:
    td = row.findAll('td')
    if td:
        player_dict = {}
        player_dict['POS'] = td[0].string
        player_dict['NAME'] = td[1].a.string
        player_dict['G'] = td[3].string
        player_dict['AB'] = td[4].string
        player_dict['AVG'] = td[16].string
        for k, v in player_dict.items():
            if player_dict[k] is None:
                player_dict[k] = "-"
        player_stats.append(player_dict)

player_stats = json.dumps(player_stats)

cursor = db.cursor()

cursor.executemany("""
    INSERT INTO test (pos, name, g, ab, avg)
    VALUES (%(POS)s, %(NAME)s, %(G)s, %(AB)s, %(AVG)s)""", player_stats)

db.commit()
以下是我试图插入数据库的
player_stats
列表中的数据:

[{"POS": "C", "NAME": "Jeff Mathis", "G": "69", "AB": "218", "AVG": ".200"}, {"POS": "1B", "NAME": "Paul Goldschmidt", "G": "158", "AB": "690", "AVG": ".290"}, {"POS": "2B", "NAME": "Ketel Marte", "G": "153", "AB": "580", "AVG": ".260"}, {"POS": "SS", "NAME": "Nick Ahmed", "G": "153", "AB": "564", "AVG": ".234"}, {"POS": "3B", "NAME": "Jake Lamb", "G": "56", "AB": "238", "AVG": ".222"}, {"POS": "LF", "NAME": "David Peralta", "G": "146", "AB": "614", "AVG": ".293"}, {"POS": "CF", "NAME": "A.J. Pollock", "G": "113", "AB": "460", "AVG": ".257"}, {"POS": "RF", "NAME": "Steven Souza Jr.", "G": "72", "AB": "272", "AVG": ".220"}, {"POS": "IF", "NAME": "Daniel Descalso", "G": "138", "AB": "423", "AVG": ".238"}, {"POS": "RF", "NAME": "Jon Jay", "G": "84", "AB": "320", "AVG": ".235"}, {"POS": "RF", "NAME": "Chris Owings", "G": "106", "AB": "309", "AVG": ".206"}, {"POS": "CF", "NAME": "Jarrod Dyson", "G": "67", "AB": "237", "AVG": ".189"}, {"POS": "C", "NAME": "Alex Avila", "G": "80", "AB": "234", "AVG": ".165"}, {"POS": "3B", "NAME": "Eduardo Escobar", "G": "54", "AB": "223", "AVG": ".268"}, {"POS": "C", "NAME": "John Ryan Murphy", "G": "87", "AB": "223", "AVG": ".202"}, {"POS": "3B", "NAME": "Deven Marrero", "G": "49", "AB": "85", "AVG": ".167"}, {"POS": "UT", "NAME": "Christian Walker", "G": "37", "AB": "53", "AVG": ".163"}, {"POS": "OF", "NAME": "Socrates Brito", "G": "24", "AB": "44", "AVG": ".175"}, {"POS": "IF", "NAME": "Ildemaro Vargas", "G": "14", "AB": "20", "AVG": ".211"}, {"POS": "LF", "NAME": "Patrick Kivlehan", "G": "9", "AB": "14", "AVG": ".231"}, {"POS": "RF", "NAME": "Kristopher Negron", "G": "2", "AB": "3", "AVG": ".333"}, {"POS": "C", "NAME": "Chris Stewart", "G": "3", "AB": "1", "AVG": ".000"}, {"POS": "P", "NAME": "Zack Greinke", "G": "30", "AB": "71", "AVG": ".234"}, {"POS": "P", "NAME": "Patrick Corbin", "G": "31", "AB": "67", "AVG": ".197"}, {"POS": "P", "NAME": "Zack Godley", "G": "32", "AB": "65", "AVG": ".057"}, {"POS": "P", "NAME": "Robbie Ray", "G": "24", "AB": "44", "AVG": ".077"}, {"POS": "P", "NAME": "Clay Buchholz", "G": "16", "AB": "37", "AVG": ".065"}, {"POS": "P", "NAME": "Matt Koch", "G": "18", "AB": "23", "AVG": ".059"}, {"POS": "P", "NAME": "T.J. McFarland", "G": "46", "AB": "7", "AVG": ".000"}, {"POS": "P", "NAME": "Shelby Miller", "G": "5", "AB": "5", "AVG": ".000"}, {"POS": "P", "NAME": "Taijuan Walker", "G": "3", "AB": "4", "AVG": ".000"}, {"POS": "P", "NAME": "Braden Shipley", "G": "3", "AB": "2", "AVG": ".000"}, {"POS": "P", "NAME": "Jorge De La Rosa", "G": "41", "AB": "2", "AVG": ".000"}, {"POS": "P", "NAME": "Yoshihisa Hirano", "G": "70", "AB": "1", "AVG": ".000"}, {"POS": "P", "NAME": "Kris Medlen", "G": "1", "AB": "1", "AVG": ".000"}, {"POS": "P", "NAME": "Matt Andriese", "G": "14", "AB": "1", "AVG": ".000"}, {"POS": "P", "NAME": "Fernando Salas", "G": "39", "AB": "1", "AVG": ".000"}, {"POS": "P", "NAME": "Troy Scribner", "G": "1", "AB": "1", "AVG": ".000"}, {"POS": "P", "NAME": "Andrew Chafin", "G": "72", "AB": "0", "AVG": "-"}, {"POS": "P", "NAME": "Archie Bradley", "G": "71", "AB": "0", "AVG": "-"}, {"POS": "P", "NAME": "Randall Delgado", "G": "10", "AB": "0", "AVG": "-"}, {"POS": "P", "NAME": "Brad Boxberger", "G": "58", "AB": "0", "AVG": "-"}, {"POS": "P", "NAME": "Jake Barrett", "G": "7", "AB": "0", "AVG": "-"}, {"POS": "P", "NAME": "Jake Diekman", "G": "24", "AB": "0", "AVG": "-"}, {"POS": "P", "NAME": "Yoan Lopez", "G": "9", "AB": "0", "AVG": "-"}, {"POS": "P", "NAME": "Silvino Bracho", "G": "30", "AB": "0", "AVG": "-"}, {"POS": "P", "NAME": "Joey Krehbiel", "G": "2", "AB": "0", "AVG": "-"}, {"POS": "P", "NAME": "Brad Ziegler", "G": "27", "AB": "0", "AVG": "-"}, {"POS": "P", "NAME": "Jimmie Sherfy", "G": "14", "AB": "0", "AVG": "-"}]

我在整个网站上搜索了几个小时,找不到哪里出了问题。我对使用MySQL和Python还很陌生,所以我可能忽略了一些简单的东西。非常感谢您的帮助。

我不熟悉
%(COL)的语法,所以我假设您不能这样做(但我不确定)

此外,
args
要求通常被认为是列表或列表列表,而不是字典列表

更改以下内容可以解决您的问题:

player_stats = [['pos','name','g','ab','avg'], ... ]

sql = 'INSERT INTO test (pos, name, g, ab, avg) VALUES (%s, %s, %s, %s, %s)'

cursor.executemany(sql, player_stats)

这可能help@Hjelpen仅供参考,您可以使用以下格式在注释中创建链接
[name](https://example.com/somelink)