我有一个脚本,该脚本使用amazon api基于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()