zoukankan      html  css  js  c++  java
  • Python Python-MySQLdb中的DictCursor使用方法简介

    Python-MySQLdb中的DictCursor使用方法简介

    by:授客 QQ1033553122

     
    
     
    
    DictCursor的这个功能是继承于CursorDictRowsMixIn,这个MixIn提供了3个额外的方法: fetchoneDictfetchmanyDictfetchallDict
    
     
    
    在默认情况下cursor方法返回的是BaseCursor类型对象,BaseCursor类型对象在执行查询后每条记录的结果以列表(list)表示。如果要返回字典(dict)表示的记录,就要设置cursorclass参数为MySQLdb.cursors.DictCursor类。
    
    cur = conn.cursor(cursorclass=MySQLdb.cursors.DictCursor)
    
     
    
    这个参数也可在调用connect方法建立连接时设置,如下:
    
    >>> conn = MySQLdb.connect(host='192.168.1.103', port=3306, user='testacc', pass
    
    wd='test1234', db='1dcq', cursorclass=MySQLdb.cursors.DictCursor)
    
    >>>conn.close()
    
     
    
    例子:
    
    >>> import MySQLdb
    
    >>> conn = MySQLdb.connect(host='192.168.1.103', port=3306, user='testacc', passwd='test1234', db='1dcq')
    
    >>> cursor = conn.cursor()
    
    >>> cursor.execute('SELECT * FROM pagesobject LIMIT 0, 1')
    
    1L
    
     
    
    >>> cursor.fetchone()
    
    (1L, 0L, '???', 'true', None, 0)
    
    >>> cursor.close()
    
     
    
    对比实验如下:
    
    >>> cursor = conn.cursor(MySQLdb.cursors.DictCursor)
    
    >>> cursor.execute('SELECT * FROM pagesobject LIMIT 0, 1')
    
    1L
    
    >>> cursor.fetchone() # 等价fetchoneDict()
    
    {'PageDesc': None, 'P_Id': 0L, 'isParent': 0, 'Html_open': 'true', 'PageName': '???', 'Id': 1L}
    
    >>> cursor.close()
    
    >>>conn.close()
    
     
    
          
    

     

  • 相关阅读:
    iOS sqlite数据库使用
    vsts 自动部署到Azure
    中国区的Azure添加到 VSTS 的 Service Endpoint
    修改vs17中的cordova模板
    升级vs17中的cordova-simulate
    cordova 从xcode7迁移到xcode8
    自杀程序&递归删除目录
    如何升级cordova插件
    在ubuntu on windows 上安装jekyll
    gitphp日期乱码解决方案
  • 原文地址:https://www.cnblogs.com/shouke/p/10157803.html
Copyright © 2011-2022 走看看