sqlalchemy在使用session.merg()时崩溃脚本

qeeaahzv  于 2021-06-19  发布在  Mysql
关注(0)|答案(0)|浏览(193)

我有一个脚本,它使用amazonapi根据upc查找项目,然后将它们保存到mysql数据库中。因为某种原因当我使用 session.merge 它随机地使脚本崩溃。使用 session.add 工作,但当数据库中有一个重复的脚本错误提交。
另外,当脚本崩溃时,它不会输出任何类型的错误。
session.merge是否会导致脚本崩溃,或者session.add是否可以更新session.commit上的重复记录?
谢谢,
代码:

upcs = <List of UPC Codes>

# Breaking the list into groups of 10 for Amazon API

group = 10
sub_upcs = [upcs[i:i+group] for i in range(0, len(upcs), group)]

for sub_upc in sub_upcs:
    try:
        products = amazon.lookup(ItemId=','.join(sub_upc), IdType='UPC', SearchIndex='All')
    except AsinNotFound as e:
        print("UPC Not Found")
    except Exception as e:
        print(e)

    if type(products) is list:
        for product in products:
            am_item = Item(product)
            session.merge(am_item.db_item)
    else:
        am_item = Item(products)
        session.merge(am_item.db_item)

try:
    session.commit()
    print("Session Written")
except Exception as e:
    print(e)

项目类别:

class AmazonItem(Base):
    __tablename__ = 'amazon_products'
    timestamp = Column(TIMESTAMP)
    itemid = Column(String, primary_key=True, index=True, unique=True)
    name = Column(String)
    upc = Column(String, index=True)
    msrp = Column(Float)
    price = Column(Float)
    brandname = Column(String)
    modelnumber = Column(String)
    url = Column(String)

    def __init__(self, item):
        self.timestamp = datetime.datetime.fromtimestamp(time.time()).strftime('%Y-%m-%d %H:%M:%S')
        self.itemid = item.asin
        self.name = item.title
        self.upc = item.upc
        self.msrp = item.price_and_currency[0]
        self.price = item.price_and_currency[0]
        self.brandname = ""
        self.modelnumber = ""
        self.url = item.offer_url

class Item():
    item = None
    db_item = None
    store = ""
    itemid = ""
    name = ""
    upc = ""
    msrp = 0
    price = 0
    brandname = ""
    modelnumber = ""

    def __init__(self, item):
        self.timestamp = datetime.datetime.fromtimestamp(time.time()).strftime('%Y-%m-%d %H:%M:%S')
        self.item = item
        self.db_item = AmazonItem(item)
        self.store = "Amazon"
        self.itemid = item.asin
        self.name = item.title
        self.upc = item.upc
        self.msrp = item.price_and_currency[0]
        self.price = item.price_and_currency[0]
        self.brandname = ""
        self.modelnumber = ""
        self.url = item.offer_url
        self.asin = item.asin

        if self.msrp == 0 or self.msrp is None:
            self.msrp = self.price

        if self.price == 0 or self.price is None:
            self.price = self.msrp

    def __eq__(self, other):
        return self.itemid == other.itemid

    def __hash__(self):
        return hash(('itemid', self.itemid))

    def writetocsv(self, file):
        csv_writer = CSVWriter(file)
        csv_writer.addrow(self.getrow())
        csv_writer.write()

暂无答案!

目前还没有任何答案,快来回答吧!

相关问题