接口主动化如何封装mysql操作

打印 上一主题 下一主题

主题 1031|帖子 1031|积分 3093

马上注册,结交更多好友,享用更多功能,让你轻松玩转社区。

您需要 登录 才可以下载或查看,没有账号?立即注册

x
数据查询类封装

1. 功能分析


  • 可以毗连不同sql数据库
  • 查一条数据,多条数据
  • 可以获取不同格式的数据
2. 封装成数据库查询类

封装思路:       

  • 数据库查询模块有多个功能,且需要复用,所以封装成类
  • 创建对象方法实现各种查询
  • 在构造方法中创建毗连 


  • 废话不多说,直接上代码!!!
     
    1. # -*- coding: utf-8 -*-
    2. # @Time : 2019/11/13 14:51
    3. # @Author : kira
    4. # @Email : 262667641@qq.com
    5. # @File : do_mysql.py
    6. # @Project : risk_project
    7. import pymysql
    8. from pymysql.cursors import DictCursor
    9. class DoMysql:
    10.     """
    11.     sql数据库查询类
    12.     """
    13.     def __init__(self, db_config: dict):
    14.         # 创建连接
    15.         engine = db_config.pop('engine', 'mysql')
    16.         if engine.lower() == 'mysql':
    17.             self.conn = pymysql.connect(**db_config)
    18.         elif engine.lower() == 'oracle':
    19.             pass
    20.     def __execute(self, sql, action, res_type='t', *args):
    21.         """
    22.         :param sql:
    23.         :param action: 字符串,指定执行cursor对应的方法
    24.         :param res_type: 返回数据类型
    25.         :param args:
    26.         :return:
    27.         """
    28.         if res_type == 't':
    29.             cursor = self.conn.cursor()
    30.         else:
    31.             cursor = self.conn.cursor(DictCursor)
    32.         try:
    33.             cursor.execute(sql)
    34.             return getattr(cursor, action)(*args)
    35.         except Exception as e:
    36.             raise e
    37.         finally:
    38.             cursor.close()
    39.     def get_one(self, sql, res_type='t'):
    40.         """
    41.         获取一条数据
    42.         :param sql:
    43.         :param res_type: 返回数据的类型,默认为t表示以元组返回,'d'表示以字典的形式返回
    44.         :return: 元组/字典
    45.         """
    46.         # 数据库若断开即重连
    47.         self.reConnect()
    48.         return self.__execute(sql, 'fetchone', res_type)
    49.     def get_many(self, sql, size, res_type='t'):
    50.         # 数据库若断开即重连
    51.         self.reConnect()
    52.         return self.__execute(sql, 'fetchmany', res_type, size)
    53.     def get_all(self, sql, res_type='t'):
    54.         # 数据库若断开即重连
    55.         self.reConnect()
    56.         return self.__execute(sql, 'fetchall', res_type)
    57.     def exist(self, sql):
    58.         if self.get_one(sql):
    59.             return True
    60.         else:
    61.             return False
    62.     def reConnect(self):
    63.         """
    64.         重连机制
    65.         :@return
    66.         """
    67.         try:
    68.             self.conn.ping()
    69.         except:
    70.             self.conn()
    71.     def __del__(self):
    72.         """
    73.         对象销毁的时候自动会被调用
    74.         :return:
    75.         """
    76.         self.conn.close()
    77. if __name__ == '__main__':
    78.     db = {
    79.         'engine': 'mysql',  # 指定mysql数据
    80.         'host': '127.0.0.1',
    81.         'user': 'admin',
    82.         'password': '12345',
    83.         'port': 3306,
    84.         'db': 'mysql',
    85.         'charset': 'utf8'
    86.     }
    87.     db = DoMysql(db)
    88.     sql = 'select id, reg_name, mobile_phone from member limit 10'
    89.     # res = db.get_one(sql)
    90.     res = db.get_many(sql, size=5)
    91.     print(res)
    复制代码
同时,在这我为各人预备了一份软件测试视频教程(含口试、接口、主动化、性能测试等),就在下方,需要的可以直接去观看。
     【2025最新版】字节大牛讲的最全最细的主动化测试全套教程!永久白嫖,拿走不谢,全程干货无废话!逼自己15天内学完,从软件测试基础到项目实战一套全通关!

免责声明:如果侵犯了您的权益,请联系站长,我们会及时删除侵权内容,谢谢合作!更多信息从访问主页:qidao123.com:ToB企服之家,中国第一个企服评测及商务社交产业平台。
回复

使用道具 举报

0 个回复

正序浏览

快速回复

您需要登录后才可以回帖 登录 or 立即注册

本版积分规则

河曲智叟

论坛元老
这个人很懒什么都没写!
快速回复 返回顶部 返回列表