美文网首页
Python代码将dict字典和datafram 格式的数据存入

Python代码将dict字典和datafram 格式的数据存入

作者: 王文强Python | 来源:发表于2019-12-19 16:42 被阅读0次
    import re
    import traceback
    import cx_Oracle
    from loglib import log
    import os
    os.environ['NLS_LANG'] = 'SIMPLIFIED CHINESE_CHINA.UTF8'
    
    try:
          ora_tns = cx_Oracle.makedsn("IP地址","1521","tzqdev")  # 创建tns
          infocenter_oracle_client = cx_Oracle.connect("账号","密码",ora_tns)  # 创建oracle client
          log.info("cx_Oracle连接成功!!")
          cursor = infocenter_oracle_client.cursor()   #创建游标
    except Exception as e:
          traceback.print_exc()
          log.error("cx_Oracle连接失败:")
    
    def write_context(dict1):
        zd = ",".join(dict1.keys())    #Key 作为字段
        val = ",".join([f"'{x}'" for x in dict1.values()])  #values 作为内容
        try:
            insert_sql = f"insert into infocenter.ESG({zd}) values ({val})"
            cursor.execute(insert_sql)
            infocenter_oracle_client.commit()
        except Exception as e:
            traceback.print_exc()
    

    传入一个dict格式的对象进去

    write_context(dict1)
    

    相关文章

      网友评论

          本文标题:Python代码将dict字典和datafram 格式的数据存入

          本文链接:https://www.haomeiwen.com/subject/vhxvnctx.html