簡體   English   中英

嘗試使用python將數據導入MySQL時,出現“ SQL語句中未使用所有參數”錯誤

[英]Getting “Not all parameters were used in the SQL statement” error, when trying to import data to MySQL using python

我正在嘗試從網站上抓取數據並將其上傳到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)s語法不熟悉,所以我假設您不能這樣做(但我不確定)。

同樣, 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)

暫無
暫無

聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM