mysql.connector.errors.ProgrammingError:SQL语句(Python,MySQL)中未使用所有参数

时间:2018-12-31 02:04:49

标签: python mysql mysql-python

问题:

我有以下代码,可以抓取数据但不能将其插入mysql。它不断给我以下错误消息:mysql.connector.errors.ProgrammingError: Not all parameters were used in the SQL statement

代码:

这是我的python脚本。

import mysql.connector
from bs4 import BeautifulSoup as soup
from selenium import webdriver
import time, re

mydb = mysql.connector.connect(
  host="host",
  user="user",
  passwd="passwd",
  database="database"
)

mycursor = mydb.cursor()

d = webdriver.Chrome('D:/Uskompuf/Downloads/chromedriver')
d.get('https://au.pcpartpicker.com/products/cpu/overall-list/#page=1')
def cpus(_source):
  result = soup(_source, 'html.parser').find('ul', {'id':'category_content'}).find_all('li')
  _titles = list(filter(None, [(lambda x:'' if x is None else x.text)(i.find('div', {'class':'title'})) for i in result]))
  data = [list(filter(None, [re.findall('(?<=\().*?(?=\))', c.text) for c in i.find_all('div')])) for i in result]
  return _titles, [a for *_, [a] in filter(None, data)]


_titles, _cpus = cpus(d.page_source)
sql = "UPDATE cpu set family = ? where name = ?"
mycursor.executemany(sql, list(zip(_titles, _cpus)))
_last_page = soup(d.page_source, 'html.parser').find_all('a', {'href':re.compile('#page\=\d+')})[-1].text
for i in range(2, int(_last_page)+1):
   d.get(f'https://au.pcpartpicker.com/products/cpu/overall-list/#page={i}') 
   time.sleep(3)
   _titles, _cpus = cpus(d.page_source)
   sql = "UPDATE cpu set family = ? where name = ?"
   mycursor.executemany(sql, list(zip(_titles, _cpus)))

mydb.commit()

我认为这与以下方面有关:

sql = "UPDATE cpu set family = ? where name = ?"
mycursor.executemany(sql, list(zip(_titles, _cpus)))

其他:

如果您需要更多信息,请告诉我

谢谢

1 个答案:

答案 0 :(得分:1)

关于您的sql语句只有一个小错误,因为库无法找到他将数据放入何处。只需将sql = "UPDATE cpu set family = ? where name = ?"更改为sql = "UPDATE cpu set family = %s where name = %s"