2

I am trying to scrape data from a website and upload it into MySQL database. But I keep getting the error:

"Not all parameters were used in the SQL statement".

Here is the code:

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

Here is the data in the player_stats list that I am trying to INSERT INTO the database:

[{"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": "-"}]

I have searched all over the site for hours and can't find where I'm going wrong. I'm quite new to working with MySQL and Python, so it might be something simple that I'm overlooking. All help is greatly appreciated.

Daniel
  • 3,312
  • 1
  • 14
  • 31
Davis Hurd
  • 39
  • 1
  • 3
  • https://stackoverflow.com/questions/20818155/not-all-parameters-were-used-in-the-sql-statement-python-mysql/20818201#20818201 This might help – Hjelpen Mar 21 '19 at 17:17
  • @Hjelpen FYI, you can create links in comments with the following format `[name](https://example.com/somelink)`. – Lord Elrond Mar 21 '19 at 18:42

1 Answers1

0

I'm not familiar with the %(COL)s syntax, so I'm assuming you can't do that (but I'm not sure).

Also, the args requirement is generally supposed to be either a list or a list of lists, and not a list of dictionaries.

changing the following should fix your problem:

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)
Lord Elrond
  • 13,430
  • 7
  • 40
  • 80