Python之cx_Oracle、psycopg2模块处理数据库数据

101 篇文章 1 订阅
# coding=utf-8
import sys
import cx_Oracle
import psycopg2
import psycopg2.extras
import datetime

conn_oracle = cx_Oracle.connect("")
print("Opened oracle successfully")
cur_oracle = conn_oracle.cursor()

conn_pg = psycopg2.connect(database='amdb', user='', password='', host='',
                           port='')
print("Opened pg successfully")
# cur_pg = conn_pg.cursor(cursor_factory=psycopg2.extras.RealDictCursor)
cur_pg = conn_pg.cursor()
def process_data(rows):
    rows = [('10.131.178.78', 'T10103', 'E4D058D6-E5CB-40FA-9A9D-04DA1D6DEF22-00064'),
            ('10.131.178.77', 'T60302', 'E4D058D6-E5CB-40FA-9A9D-04DA1D6DEF22-00055')]
    for row in rows:
        #数据处理关键代码
        ip_list.append("""\'%s\'""" % str(row[0]))
    pg_sql = "select resourceid from am_managedobject where resourcename in (%s)" % (",".join(ip_list))
    return pg_sql
def read_COLLECT(id_list):
    """读取COLLECT表"""
    ids = (",".join(id_list))
    collect_config_sql = "SELECT COLL_FUNC from COLLECT  where AGENT_CODE='APM_PG'"
    collect_config = exec_oracle_sql(collect_config_sql)
    result_list = []
    for cfg in collect_config:
       #数据库的COLL_FUNC 形式为select resourceid from am_managedobject where resourcename in (%s)
        result = exec_pg_sql(cfg[0] % ids)
        result_list.append(result)
    return result_list
def get_some_info(new_dict, attr_code, result, perf_attr):
    """获取操作系统信息,插入数据库"""
    print(new_dict, attr_code, result, perf_attr)
    #Oracle数据库处理日期方法
    dt = datetime.datetime.now()
    perf_attr_sql = ""
    for resids in result:
        for resid in resids:
            for key in new_dict.keys():
                if int(key) == resid[0]:
                    params = {'devtype': new_dict[key][1], 'devid': new_dict[key][2], 'attrcode': attr_code,
                              'result': resid[1], 'dt': dt}
                    if perf_attr == 2:
                        try:
                            cur_oracle.execute(
                                'insert into ATTR_RESULT values(:devtype,:devid,:attrcode,:result,:dt)',
                                params)
                            print("配置数据插入成功!成功数据为:%s" % params)
                        except cx_Oracle.IntegrityError as e:
                            print("配置数据插入失败,重复插入!失败数据为:%s" % params)
                    elif perf_attr == 1:
                        try:
                            cur_oracle.execute(
                                'insert into PERF_RESULT values(:devtype,:devid,:attrcode,:result,:dt)',
                                params)
                            print("性能数据插入成功!成功数据为:%s" % params)
                        except cx_Oracle.IntegrityError as e:
                            print("性能数据插入失败,重复插入!失败数据为:%s" % params)

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值