【发布时间】:2019-08-27 21:48:26
【问题描述】:
已修复
对于一个教育项目,我试图将抓取的数据存储在 MS SQL 数据库中。首先,我希望将每件独特的物品放在products_tb 中。插入唯一产品后,SQL 必须为所述项目生成唯一 ID,即productgroupid。 products_tb 表只会产生永远不会改变的产品信息,例如productid, category, name and description。在我得到这个工作后我将创建的第二个表中,我将存储以下数据:productgroupid, price, timestamp。原因是这些可能会不时改变。使用productgroupid,我总是可以在任何给定时间对所有数据进行分组并创建图表等等。
问题是我无法让我的pipelines.py 工作。但是我确实设法使用注释的代码块将数据插入到我的 SQL 数据库中:
# self.cursor.execute("INSERT INTO products_tb(productid, category, name, description, price, timestamp) VALUES (%s, %s, %s, %s, %s, %s)",
# (item['productid'], item['category'], item['name'], item['description'], item['price'], item['timestamp']))
似乎正在使用以下代码
pipelines.py
import pymssql
class KrcPipeline(object):
def __init__(self):
self.conn = pymssql.connect(host='DESKTOP-P1TF28R', user='sa', password='123', database='kaercher')
self.cursor = self.conn.cursor()
def process_item(self, item, spider):
# self.cursor.execute("INSERT INTO products_tb(productid, category, name, description, price, timestamp) VALUES (%s, %s, %s, %s, %s, %s)",
# (item['productid'], item['category'], item['name'], item['description'], item['price'], item['timestamp']))
sql_statement = f'''
BEGIN
IF NOT EXISTS (SELECT * FROM [kaercher].[dbo].[products_tb]
WHERE productid = {item['productid']})
BEGIN
INSERT INTO [kaercher].[dbo].[products_tb] (productid, category, name, description)
OUTPUT (Inserted.productgroupid)
VALUES ({item['productid']}, '{item['category']}', '{item['name']}', '{item['description']}')
END
ELSE
BEGIN
SELECT productgroupid FROM [kaercher].[dbo].[products_tb]
WHERE productid = {item['productid']}
END
END
'''
self.cursor.execute(sql_statement)
self.conn.commit()
return item
items.py
import scrapy
class KrcItem(scrapy.Item):
productid=scrapy.Field()
name=scrapy.Field()
description=scrapy.Field()
price=scrapy.Field()
producttype=scrapy.Field()
timestamp=scrapy.Field()
category=scrapy.Field()
pass
【问题讨论】: