def fetch_data(): with open(os.path.join(os.getcwd(), 'a.json')) as f: result = json.loads(f.read()) for res in result: print(f"save {res.get('RegionName')}") try: area = Areas(**res) dbSession.add(area) dbSession.commit() except: continue
def update_and_insert(cls, **kwargs): name = kwargs.get('companyName') row = dbSession.query(cls) \ .filter(Company.companyName == name) \ .first() if row: logger.debug(f"{name} 已经存在 更新数据") try: for k, v in kwargs.items(): setattr(row, k, v) dbSession.commit() except Exception as e: logger.error("Update Error " + str(e)) else: logger.debug(f"{name} 不存在 新增数据") try: new_row = Company(**kwargs) dbSession.add(new_row) dbSession.commit() except Exception as e: logger.error("Insert Error " + str(e))
def update_and_insert(cls, **kwargs): province = kwargs.get('provinceName') city = kwargs.get('cityName') row = dbSession.query(cls) \ .filter(and_(SariRecord.provinceName.like('%{}%'.format(province)), SariRecord.cityName == city)) \ .first() if row: logger.debug("已经存在 更新数据") try: for k, v in kwargs.items(): setattr(row, k, v) dbSession.commit() except Exception as e: logger.error("Update Error " + str(e)) else: logger.debug("不存在 新增数据") try: new_row = SariRecord(**kwargs) dbSession.add(new_row) dbSession.commit() except Exception as e: logger.error("Insert Error " + str(e))
def update_and_insert(cls, **kwargs): title = kwargs.get('title') pubDate = int(str( kwargs.get('pubDate'))[:-3]) if kwargs.get('pubDate') else None row = dbSession.query(cls) \ .filter(and_(SariNews.title == title, SariNews.pubDate == pubDate)) \ .first() if row: logger.debug("新闻 已经存在 更新数据") try: for k, v in kwargs.items(): setattr(row, k, v) dbSession.commit() except Exception as e: logger.error("Update Error " + str(e)) else: logger.debug("新闻 不存在 新增数据") try: new_row = SariNews(**kwargs) dbSession.add(new_row) dbSession.commit() except Exception as e: logger.error("Insert Error " + str(e))
def update_and_insert(cls, **kwargs): infectSource = kwargs.get('infectSource') updateTime = int(str(kwargs.get('updateTime'))[:-3]) if kwargs.get( 'updateTime') else None row = dbSession.query(cls) \ .filter(and_(SariOverall.infectSource == infectSource, SariRecord.updateTime == updateTime)) \ .first() if row: logger.debug("头条 已经存在 更新数据") try: for k, v in kwargs.items(): setattr(row, k, v) dbSession.commit() except Exception as e: logger.error("Update Error " + str(e)) else: logger.debug("头条 不存在 新增数据") try: new_row = SariOverall(**kwargs) dbSession.add(new_row) dbSession.commit() except Exception as e: logger.error("Insert Error " + str(e))
def add(cls, **kwargs): """增加一行数据""" new_row = Areas(**kwargs) dbSession.add(new_row) dbSession.commit() return new_row
def add(cls, **kwargs): """增加一行数据""" new_row = OrderUser(**kwargs) dbSession.add(new_row) dbSession.commit() return new_row