python - mysql.connector.errors.ProgrammingError : Not all parameters were used in the SQL statement (Python, MySQL)

标签 python mysql mysql-python

问题:

我有以下代码,可以抓取数据但不能将其插入到 mysql 中。它不断向我提供以下错误消息:mysql.connector.errors.ProgrammingError:并非所有参数都在 SQL 语句中使用

代码:

这是我的 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)))

其他:

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

谢谢

最佳答案

只是你的sql语句有一个小错误,因为库找不到他将数据放入的位置。只需要将 sql = "UPDATE cpu set family = ? where name = ?" 更改为 sql = "UPDATE cpu set family = %s where name = %s"

关于python - mysql.connector.errors.ProgrammingError : Not all parameters were used in the SQL statement (Python, MySQL),我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/53982940/

相关文章:

Python:矩阵的非对角线元素为0

php - 插入的数据不会进入 phpMyAdmin 数据库

python - 将列表插入mysql数据库

Python、带有变量的 mySQL

python - 为什么我收到 MySQL 语法错误?

python - 上传图片时 Dropbox API 错误 (Python)

python - Python 多参数返回语法糖吗?

当我运行脚本时,Python 3.6 不是默认的

MYSQL:根据条件在一个字段或另一个字段上连接两个表

mysql - 相当于 HQL 中的 GREATEST()