zoukankan      html  css  js  c++  java
  • 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发布
         
       

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

  • 相关阅读:
    浏览器操作本地缓存记录一下
    dotnet new Getting ready... Object reference not set to an instance of an object.
    IIS上vue打包后接口跨域解决
    SpringBoot前言
    Node聊天室和socket.io原理与功能总结
    Node加解密原理和功能探索总结
    Node中文件断点续传原理和方法总结
    Node短链原理与功能实现总结
    Node中F2A原理及功能实现总结
    Node图形邮箱手机验证码实现方法总结
  • 原文地址:https://www.cnblogs.com/shurun/p/11956828.html
Copyright © 2011-2022 走看看