【问题标题】:Error inserting data into MS SQL DB Using Pymssql使用 Pymssql 将数据插入 MS SQL DB 时出错
【发布时间】:2019-08-27 21:48:26
【问题描述】:

已修复

对于一个教育项目,我试图将抓取的数据存储在 MS SQL 数据库中。首先,我希望将每件独特的物品放在products_tb 中。插入唯一产品后,SQL 必须为所述项目生成唯一 ID,即productgroupidproducts_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

【问题讨论】:

    标签: sql scrapy pymssql


    【解决方案1】:

    编辑:

    我错过的另一个小错误。 “IF NOT EXIST”需要改为“IF NOT EXISTS”。

    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
    

    原文:

    在定义 sql_statement 时,您没有正确调用项目字典中的值。试试这个:

    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 EXIST (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
    

    【讨论】:

    • 您好@Samlegesse,感谢您的解决方案,我非常感谢您,但使用您提供的代码时,我仍然遇到完全相同的错误。
    • @Bamieschijf 我注意到您遇到操作错误的位置附近有一个错字。请参阅编辑后的答案。
    • 您好@Samlegesse,再次感谢您的反馈。已经取得了进展,但现在我得到了一个不同的语法错误。我在几个 MySQL 语法检查器中导入了代码,他们都一直说查询的第一部分有问题,即: IF NOT EXISTS (SELECT * FROM [ kaercher].[dbo].[products_tb] WHERE productid = {item['productid']})。我在问题中编辑了我的代码等(见上文)
    • @Bamieschijf 您正在使用 MySQL 语法检查器进行 SQL Server 查询?根据您现在收到的新错误,问题似乎在 productid 附近。 productid 是 varchar 还是 int 类型?如果是 varchar,则需要用引号括起来。
    • 你好 @Samlegesse 我提到了 SQL 语法检查器,这是我的错字。我做了一些更改,数据现在存储在我的数据库中。我不知道这是否是最好的解决方案,但到目前为止数据都存储在我的数据库中。我要感谢您的帮助! :-)
    猜你喜欢
    • 1970-01-01
    • 2018-06-04
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2019-08-05
    • 1970-01-01
    • 2018-01-23
    相关资源
    最近更新 更多