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

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

  • 相关阅读:
    定义结构体
    UML建模需求分析常用的UML图
    UML建模EA模型的组织
    优化Python脚本替换VC2005/2008工程x64配置
    C++插件框架已在 Mac OS X 下编译通过
    《iPhone开发快速入门》交流提纲
    X3插件框架发布v1.1.3
    从零开始创建一个插件
    Google论坛什么时候又可以使用的
    我的第一个Py脚本:批量替换VC工程中的x64条件定义配置
  • 原文地址:https://www.cnblogs.com/shurun/p/11956828.html
Copyright © 2011-2022 走看看