• python模块-pymysql源码分析及其常见使用


    D:softpython36Libsite-packagespymysql>tree /f
    卷 新加卷 的文件夹 PATH 列表
    卷序列号为 960E-961E
    D:.
    │  charset.py
    │  connections.py
    │  converters.py
    │  cursors.py
    │  err.py
    │  optionfile.py
    │  protocol.py
    │  times.py
    │  util.py
    │  _auth.py
    │  _compat.py
    │  _socketio.py
    │  __init__.py
    │
    ├─constants
    │  │  CLIENT.py
    │  │  COMMAND.py
    │  │  CR.py
    │  │  ER.py
    │  │  FIELD_TYPE.py
    │  │  FLAG.py
    │  │  SERVER_STATUS.py
    │  │  __init__.py
    │  │
    │  └─__pycache__
    │          CLIENT.cpython-36.pyc
    │          COMMAND.cpython-36.pyc
    │          CR.cpython-36.pyc
    │          ER.cpython-36.pyc
    │          FIELD_TYPE.cpython-36.pyc
    │          FLAG.cpython-36.pyc
    │          SERVER_STATUS.cpython-36.pyc
    │          __init__.cpython-36.pyc
    │
    └─__pycache__
            charset.cpython-36.pyc
            connections.cpython-36.pyc
            converters.cpython-36.pyc
            cursors.cpython-36.pyc
            err.cpython-36.pyc
            optionfile.cpython-36.pyc
            protocol.cpython-36.pyc
            times.cpython-36.pyc
            util.cpython-36.pyc
            _auth.cpython-36.pyc
            _compat.cpython-36.pyc
            _socketio.cpython-36.pyc
            __init__.cpython-36.pyc
    pymysql源码目录结构
    # -*- coding: utf-8 -*-
    # @Time : 2019/8/14 10:38
    # @Author : wangmengmeng
    import pymysql
    from config.cfg import *
    
    
    class ConnectDB:
        def __init__(self):
            print('start connecting MySQL...')
            try:
                self.host = host
                self.port = int(port)
                self.username = user_name
                self.password = pwd
                self.db_sf_full = db_auditcenter
            except Exception as e:
                print(e)
            else:
                print("数据库连接成功!")
    
        def connect(self):
            return pymysql.Connect(host=self.host, port=self.port, user=self.username, passwd=self.password,
                                   database=self.db_sf_full, charset='utf8')
    
    
    
    if __name__ == '__main__':
        con = ConnectDB()
        # cur = con.connect().cursor() # 返回格式为 元组里嵌套元组
        """
        如:((1002, '3', 'wangmm', '王萌萌', '122', '122', 0, datetime.datetime(2020, 3, 9, 14, 28, 18), None), (12002, '3', 'wangmm', '王萌萌', 'vvv', 'vvv', 0, datetime.datetime(2020, 3, 13, 13, 43, 11), None))
        """
        cur = con.connect().cursor(pymysql.cursors.DictCursor) # 返回格式为 列表里嵌套字典
        """
        如:[{'id': 1002, 'pharmacist_id': '3', 'pharmacist_name': 'wangmm', 'pharmacist_realname': '王萌萌', 'tag': '122', 'tag_py': '122', 'default_flag': 0, 'created_time': datetime.datetime(2020, 3, 9, 14, 28, 18), 'modified_time': None}, {'id': 12002, 'pharmacist_id': '3', 'pharmacist_name': 'wangmm', 'pharmacist_realname': '王萌萌', 'tag': 'vvv', 'tag_py': 'vvv', 'default_flag': 0, 'created_time': datetime.datetime(2020, 3, 13, 13, 43, 11), 'modified_time': None}]
        """
        cur.execute('SELECT * FROM `sf_collect_tag`')
        # print(cur.fetchone())  # 查询一条数据,返回一维元组,若查询结果集有多条,则只会取最上面一条,若没有结果则返回None
        # print(cur.fetchall())  # 查询多条数据,返回二维元组,若没有结果则返回()
        print(cur.fetchmany(2))  # 查询指定条数据
  • 相关阅读:
    STM32 -- 硬件知识
    PCIe相关的操作命令
    [转载]PCI/PCIe基础——配置空间
    [转载]网络虚拟化中的 offload 技术:LSO/LRO、GSO/GRO、TSO/UFO、VXLAN
    [转载]TSO、UFO、GSO、LRO、GRO和RSS介绍
    Linux应用函数 -- 字符串
    初级PLC
    中断方式下进行串口通讯的正确方法
    [altium] Altium Designer2013 13.3.4 (10.1881.28608) 完美版
    32个最热CPLD-FPGA论坛
  • 原文地址:https://www.cnblogs.com/wang-mengmeng/p/12485212.html
Copyright © 2020-2023  润新知