上代码:

 
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发布
     
   

 

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

posted on 2019-11-29 14:37  十行道人  阅读(1711)  评论(0编辑  收藏  举报