mysql error python -mysql.connector.errors.ProgrammingError:SQL语句的参数不足

o3imoua4  于 2023-05-16  发布在  Mysql
关注(0)|答案(1)|浏览(146)

我正在管道中运行一个类,以将我的scrappy输出保存到数据库,但我得到一个错误

文件“C:\Users\BRAINBOX\Downloads\freeCodeCamp-part-6-main\freeCodeCamp-part6\bookscraper\pipelines. py”,第63行,在process_item self中。cur. execute(sql,row)文件"c:\users\brainbox\appdata\local\programs\python\python38 - 32\lib\site-packages\mysql\connector\cursor.py",第598行,在execute stmt = RE_PY_PARAM中。sub(psub,stmt)文件"c:\users\brainbox\appdata\local\programs\python\python38 - 32\lib\site-packages\mysql\connector\cursor.py",line 127,in_* call *_raise ProgrammingError(mysql.连接器错误编程错误:SQL语句参数不足
这就是我得到的错误。

代码如下:

# Define your item pipelines here
#
# Don't forget to add your pipeline to the ITEM_PIPELINES setting
# See: https://docs.scrapy.org/en/latest/topics/item-pipeline.html
import mysql.connector

# useful for handling different item types with a single interface
from itemadapter import ItemAdapter

class BookscraperPipeline:
    def process_item(self, item, spider):
        adapter = ItemAdapter(item)
        field_names = adapter.field_names()
        for field_name in field_names:
            value = adapter.get(field_name)
            if type(value) is tuple and "£" in value:
                adapter[field_name] = value[0].replace("£", "")
            elif type(value) is not tuple and "£" in value:
                adapter[field_name] = value.replace("£", "")
        return item

class SaveToMysqlPipeline:
    def __init__(self):
        self.con = mysql.connector.connect(
            host="localhost",
            user="root",
            password="09.02girl",
            database="books",
        )
        self.cur = self.con.cursor()
        self.cur.execute("""
        CREATE TABLE IF NOT EXISTS books(
        id int NOT NULL auto_increment,
        url VARCHAR(225),
        title text,
        product_type VARCHAR(255),
        price_excl_tax DECIMAL,
        price_incl_tax DECIMAL,
        tax DECIMAL,
        price DECIMAL,
        availability VARCHAR(255),
        num_reviews VARCHAR(255),
        stars VARCHAR(255),
        category VARCHAR(225),
        description text,
        PRIMARY KEY(id)
        )

        """)

    def process_item(self, item, spider):
        rows = (
            item['url'], item['title'], item['product_type'], item['price_excl_tax'], item['price_incl_tax'],
            item['tax'], item['availability'], item['num_reviews'], item['stars'], item['category'],
            item['description'], item['price'])
        sql = "INSERT INTO books(url, title, product_type, price_excl_tax, price_incl_tax,tax, price, availability,"\
              "num_reviews, stars, category, description) VALUES(%s, %s, %s, %s, %s, %s, %s, %s, %s, %s, %s, %s)"
        for row in rows:
            self.cur.execute(sql, row)
        self.con.commit()
        return item

    def closespider(self, spider):
        self.cur.close()
        self.con.close()
hs1ihplo

hs1ihplo1#

首先,让我们了解一下rows是什么:

rows = (
            item['url'], item['title'], item['product_type'], item['price_excl_tax'], item['price_incl_tax'],
            item['tax'], item['availability'], item['num_reviews'], item['stars'], item['category'],
            item['description'], item['price'])

它是一组12个值。现在,循环rows如下

for row in rows:
            self.cur.execute(sql, row)

并尝试为rows中的12个值中的每个值执行脚本,因此每次向execute()传递一个参数。
但是,您的查询需要正好12个参数:

sql = "INSERT INTO books(url, title, product_type, price_excl_tax, price_incl_tax,tax, price, availability,"\
              "num_reviews, stars, category, description) VALUES(%s, %s, %s, %s, %s, %s, %s, %s, %s, %s, %s, %s)"

因此,这个方法已经有了一个row from rows(item的名称清楚地表明了这一点),因此,您应该能够执行它,如

self.cur.execute(sql, item['url'], item['title'], item['product_type'], item['price_excl_tax'], item['price_incl_tax'], item['tax'], item['availability'], item['num_reviews'], item['stars'], item['category'], item['description'], item['price'])

但是不要忘记在调用process_item时使用循环。

相关问题