[英]Getting “Not all parameters were used in the SQL statement” error, when trying to import data to MySQL using python
I am trying to scrape data from a website and upload it into MySQL database. 我正在尝试从网站上抓取数据并将其上传到MySQL数据库。 But I keep getting the error:
但我不断收到错误:
"Not all parameters were used in the SQL statement".
“并非在SQL语句中使用了所有参数”。
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: 这是我试图插入数据库的
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": "-"}]
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.
我对使用MySQL和Python很陌生,因此我可能忽略了一些简单的事情。 All help is greatly appreciated.
非常感谢所有帮助。
I'm not familiar with the %(COL)s
syntax, so I'm assuming you can't do that (but I'm not sure). 我对
%(COL)s
语法不熟悉,所以我假设您不能这样做(但我不确定)。
Also, the args
requirement is generally supposed to be either a list or a list of lists, and not a list of dictionaries. 同样,
args
要求通常应该是列表或列表列表,而不是字典列表。
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)
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.