def load_declaration(cnx_info, file_name):
    primary_key, root_element = etd.init(file_name)
    if root_element is None or primary_key is None or primary_key == '':
        logger.error('Fail to extract data from file: ' + file_name)
        return

    logger.info('Updating database (primaryKey: {})'.format(primary_key))

    try:
        cnx = psycopg2.connect(**cnx_info)
        cnx.autocommit = False
        cur = cnx.cursor()

        data = etd.get_organization_info_dic(root_element)
        data.update(etd.get_procurement_info_dic(root_element))
        data.update(etd.get_declaration_info_dic(root_element))
        data.update(etd.get_attend_info_dic(root_element))
        data.update(etd.get_other_info_dic(root_element))
        data['primary_key'] = primary_key

        cur.execute(gen_insert_sql('tender_declaration_info', data))
        cnx.commit()
    except psycopg2.Error as e:
        outstr = 'Fail to update database (primary_key: {})\n\t{}'.format(primary_key, e)
        logger.warn(outstr)
        with open('load.err', 'a', encoding='utf-8') as err_file:
            err_file.write(outstr)
        cnx.rollback()
    except AttributeError as e:
        outstr = 'Corrupted content. Update skipped (primary_key: {})\n\t{}'.format(primary_key, e)
        logger.warn(outstr)
        with open('load.err', 'a', encoding='utf-8') as err_file:
            err_file.write(outstr)
    else:
        cnx.close()
示例#2
0
def load_declaration(cnx_info, file_name):
    primary_key, root_element = etd.init(file_name)
    if root_element is None or primary_key is None or primary_key == '':
        logger.error('Fail to extract data from file: ' + file_name)
        return

    logger.info('Updating database (primaryKey: {})'.format(primary_key))

    try:
        cnx = mysql.connector.connect(**cnx_info)
        cnx.autocommit = False
        cur = cnx.cursor(buffered=True)

        data = etd.get_organization_info_dic(root_element)
        data.update(etd.get_procurement_info_dic(root_element))
        data.update(etd.get_declaration_info_dic(root_element))
        data.update(etd.get_attend_info_dic(root_element))
        data.update(etd.get_other_info_dic(root_element))
        data['primary_key'] = primary_key

        cur.execute('SET NAMES utf8mb4')
        cur.execute(gen_insert_sql('tender_declaration_info', data))
        cnx.commit()
    except mysql.connector.Error as e:
        if e.errno == errorcode.ER_ACCESS_DENIED_ERROR:
            logger.error("Something is wrong with your user name or password.")
        elif e.errno == errorcode.ER_BAD_DB_ERROR:
            logger.error("Database does not exist.")
        else:
            outstr = 'Fail to update database (primary_key: {})\n\t{}'.format(
                primary_key, e)
            logger.warn(outstr)
            with open('load.err', 'a', encoding='utf-8') as err_file:
                err_file.write(outstr)
    except AttributeError as e:
        outstr = 'Corrupted content. Update skipped (primary_key: {})\n\t{}'.format(
            primary_key, e)
        logger.warn(outstr)
        with open('load.err', 'a', encoding='utf-8') as err_file:
            err_file.write(outstr)
    else:
        cnx.close()
def load_declaration(cnx_info, file_name):
    primary_key, root_element = etd.init(file_name)
    if root_element is None or primary_key is None or primary_key == '':
        logger.error('Fail to extract data from file: ' + file_name)
        return

    logger.info('Updating database (primaryKey: {})'.format(primary_key))

    try:
        cnx = mysql.connector.connect(**cnx_info)
        cnx.autocommit = False
        cur = cnx.cursor(buffered=True)

        data = etd.get_organization_info_dic(root_element)
        data.update(etd.get_procurement_info_dic(root_element))
        data.update(etd.get_declaration_info_dic(root_element))
        data.update(etd.get_attend_info_dic(root_element))
        data.update(etd.get_other_info_dic(root_element))
        data['primary_key'] = primary_key

        cur.execute('SET NAMES utf8mb4')
        cur.execute(gen_insert_sql('tender_declaration_info', data))
        cnx.commit()
    except mysql.connector.Error as e:
        if e.errno == errorcode.ER_ACCESS_DENIED_ERROR:
            logger.error("Something is wrong with your user name or password.")
        elif e.errno == errorcode.ER_BAD_DB_ERROR:
            logger.error("Database does not exist.")
        else:
            outstr = 'Fail to update database (primary_key: {})\n\t{}'.format(primary_key, e)
            logger.warn(outstr)
            with open('load.err', 'a', encoding='utf-8') as err_file:
                err_file.write(outstr)
    except AttributeError as e:
        outstr = 'Corrupted content. Update skipped (primary_key: {})\n\t{}'.format(primary_key, e)
        logger.warn(outstr)
        with open('load.err', 'a', encoding='utf-8') as err_file:
            err_file.write(outstr)
    else:
        cnx.close()
示例#4
0
def load_declaration(cnx_info, file_name):
    primary_key, root_element = etd.init(file_name)
    if root_element is None or primary_key is None or primary_key == '':
        logger.error('Fail to extract data from file: ' + file_name)
        return

    logger.info('Updating database (primaryKey: {})'.format(primary_key))

    try:
        cnx = psycopg2.connect(**cnx_info)
        cnx.autocommit = False
        cur = cnx.cursor()

        data = etd.get_organization_info_dic(root_element)
        data.update(etd.get_procurement_info_dic(root_element))
        data.update(etd.get_declaration_info_dic(root_element))
        data.update(etd.get_attend_info_dic(root_element))
        data.update(etd.get_other_info_dic(root_element))
        data['primary_key'] = primary_key

        cur.execute(gen_insert_sql('tender_declaration_info', data))
        cnx.commit()
    except psycopg2.Error as e:
        outstr = 'Fail to update database (primary_key: {})\n\t{}'.format(
            primary_key, e)
        logger.warn(outstr)
        with open('load.err', 'a', encoding='utf-8') as err_file:
            err_file.write(outstr)
        cnx.rollback()
    except AttributeError as e:
        outstr = 'Corrupted content. Update skipped (primary_key: {})\n\t{}'.format(
            primary_key, e)
        logger.warn(outstr)
        with open('load.err', 'a', encoding='utf-8') as err_file:
            err_file.write(outstr)
    else:
        cnx.close()