D:\soft\python36\Lib\site-packages\pymysql>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
# -*- 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)) # 查詢指定條數據