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


    上代码:

    import SqlHelper.MSSQL as MS
    import  pandas  as pd
    if __name__ == '__main__': 
        #连接数据库
        ms = MS.MSSQL(host="***.***.***.***",user="**",pwd="**",db="**")
    
        ########################################################## 返回无表头数据
        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发布
         
       

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

  • 相关阅读:
    Android 百度地图开发(一)--- 申请API Key和在项目中显示百度地图
    Session,Cookie,jsessionid,Url重写
    PHP输出当前进程所有变量 / 常量 / 模块 / 函数 / 类
    table自适应宽度
    python学习笔记1(字符串操作)
    jquery-select选中
    dos命令大全
    dos命令之创建文件,文件夹
    PHP中逻辑运算符的高效用法---&&和||
    apache vhost
  • 原文地址:https://www.cnblogs.com/shurun/p/11956828.html
Copyright © 2020-2023  润新知