def db_find_min_code(item_key): db_session = DBFactory().get_db_session() query = db_session.query(DBItem.item_code) result = query.order_by(DBItem.item_code.asc()).filter(DBItem.item_key == item_key).limit(1).first() if result is not None: return result.item_code else: return item_key + '-001'
def func2(key, attribute): db_session = DBFactory().get_db_session() # query_sql = 'SELECT item_date from items where item_key = :item_key order by item_code desc' # db_session.execute(query_sql, {'item_key': item_key}) query = db_session.query(attribute).filter(DBKey.key_ == key) print(query) result = query.first() print(result[0])
def db_save(item): db_session = DBFactory.get_db_session() db_item = DBItemService.copy_to_db(item) db_session.add(db_item) Statistic.db_item_success() logging.info(item.get_code + "已写入数据库缓存") return
def db_commit(db_key): db_session = DBFactory.get_db_session() try: db_session.flush() db_session.commit() logging.info(db_key + "已提交数据库'keys_info'") except IntegrityError as e: db_session.rollback() Statistic.db_key_failed() logging.error("记录重复") logging.error(e) except Exception as e: Statistic.db_key_failed() logging.error("数据库写入失败!") logging.error(e)
def db_if_code_exist(item_code): db_session = DBFactory().get_db_session() query = db_session.query(func.count('*')) result = query.filter(DBItem.item_code == item_code).first() return result[0]
def db_if_key_exist(key): db_session = DBFactory().get_db_session() query = db_session.query(func.count('*')) result = query.filter(DBKey.key_ == key).first() return result[0]
def db_find_attribute(key, attribute): db_session = DBFactory().get_db_session() query = db_session.query(attribute).filter(DBKey.key_ == key) result = query.first() return result[0]
def db_update(db_key): db_session = DBFactory.get_db_session() db_session.merge(db_key) Statistic.db_key_success() logging.info(db_key.key_ + "已写入数据库缓存") return
def db_find_key_total(item_key): db_session = DBFactory().get_db_session() query = db_session.query(func.count('*')) result = query.filter(DBItem.item_key == item_key).first() return result[0]
def db_find_attribute(item_code, attribute): db_session = DBFactory().get_db_session() query = db_session.query(attribute).filter(DBItem.item_code == item_code) logging.info(query) result = query.first() return result[0]
def db_find_max_date(item_key): db_session = DBFactory().get_db_session() query = db_session.query(DBItem.item_date) result = query.order_by(DBItem.item_code.desc()).filter(DBItem.item_key == item_key).limit(1).first() return result.item_date
from Entity.Statistic import Statistic from Entity.Status import Status from Factory.DBFactory import DBFactory from Factory.LogFactory import LogFactory from Service.ConfigService import ConfigService from Service.DBItemService import DBItemService from Service.DBKeyService import DBKeyService from Service.ItemService import ItemService from Service.NetworkService import NetworkService from Service.StatisticsService import StatisticsService from Utility.Utility import Utility ######################################################################################################## warnings.filterwarnings("ignore") ConfigService().init() DBFactory() Statistic() LogFactory().init() retry = int() ######################################################################################################## def search_by_code(item, image, dst_dir): result = Status.ERROR sleep(1) db = DBItemService.db_if_code_exist(item.get_code) try: code = item.get_code item_html = NetworkService.get_search_html(code) soup = BeautifulSoup(item_html.text, "html.parser")