• python处理oracle数据库的返回数据


    上代码:

     
    import SqlHelper.ORACLE as ORA
    import  pandas  as pd
    if __name__ == '__main__': 
        #连接数据库
        ms = ORA.ORACLE(host="localhost:1521",db="orcl",user="example",pwd="example")
    
        ########################################################## 返回无表头数据列表
        reslist = ms.ExecQuery("select * from version ")
        for x in reslist:
            print(x)
        #输出结果:
        #(1, '1.0.0.0', '初始版本')
        #(2, '1.0.0.1', '新版本,2019-10-09 16:35:00发布')
        #(3, '1.0.0.2', None)
        #(4, '1.0.0.3', None)
    
        ########################################################## 返回有表头数据列表DataFrame
        df = ms.ExecQueryToDataFrame("select * from version ")
        print(df)
        #输出结果:
        #   id  version                    message
        #0   1  1.0.0.0                       初始版本
        #1   2  1.0.0.1  新版本,2019-10-09 16:35:00发布
        #2   3  1.0.0.2                       None
        #3   4  1.0.0.3                       None
        
        ########################################################## 遍历DataFrame数据,取version、message字段
        #方式一
        for row in df.itertuples():
            print(getattr(row, 'VERSION'), getattr(row, 'MESSAGE')) 
        #输出结果:
        #1.0.0.0 初始版本
        #1.0.0.1 新版本,2019-10-09 16:35:00发布
        #1.0.0.2 None
        #1.0.0.3 None
      
        #方式二
        for i in range(0, len(df)):
            print(df.iloc[i]['VERSION'], df.iloc[i]['MESSAGE'])
        #输出结果:
        #1.0.0.0 初始版本
        #1.0.0.1 新版本,2019-10-09 16:35:00发布
        #1.0.0.2 None
        #1.0.0.3 None
    
        ########################################################### 取第2行数据
        print(df.iloc[1])   #两列,左边是键,右边是值
        #输出结果:
        #id                                 2
        #version                      1.0.0.1
        #message    新版本,2019-10-09 16:35:00发布
        #Name: 1, dtype: object
         
    
        ########################################################### 取第2行的message字段值
        print(df.iloc[1]['MESSAGE']) 
        #输出结果:
        #新版本,2019-10-09 16:35:00发布
         
       

    如果对您有帮助,请赞助根棒棒糖~

  • 相关阅读:
    ansible
    celery 计划任务使用
    11 session 使用
    10 模版继承和页面之间的调用
    9 模版语言 jinja2
    8 公共函数
    7 文件上传
    6 获取请求头和URL信息
    5 获取Form表单取值
    4 文件操作 支持图片 视频 mp3 文本等
  • 原文地址:https://www.cnblogs.com/shurun/p/11957641.html
Copyright © 2020-2023  润新知