初识httprunner之读取mysql配置文件以及连接mysql

import os, configparser


class ConfigReader():
    def __init__(self, file_name):
        self.config = configparser.ConfigParser()
        config_path = os.path.split(os.path.realpath(__file__)[0] + os.sep + file_name)  # todo:定位文件位置
        self.config.read(config_path, "utf-8")  # todo:读取文件编码格式为utf-8

    def get_option(self, _option):
        sections = self.config.sections()
        for i in sections:
            if self.config.has_option(i, _option):
                return self.config.get(i, _option)


class DB():
    def __init__(self, DBname):
        cf = ConfigReader("databases_config_user.cfg")   # todo:读取指定配置文件,如果有多个配置文件还得改
        self.host = cf.get_option(DBname + "_host")  # todo:获取具体的host地址
        self.port = cf.get_option(DBname + "_port")  # todo:获取具体的port端口号
        self.user = cf.get_option(DBname + "_user")  # todo:获取具体的user账号
        self.password = cf.get_option(DBname + "_password")  # todo:获取具体的数据库密码
        self.DBname = DBname

    def connect(self):  # todo:上面的所有代码都在为这个方法做铺垫,也就是python连接数据库
        import pymysql
        self.db = pymysql.connect(host=self.host, user=self.user, password=self.password, database=self.DBname)
        self.cursor = self.db.cursor()  # todo:创建游标

    def close(self):
        self.cursor.close()
        self.db.close()  # todo:连接数据库时游标打开,打开就得关闭,别问为啥,出错了就送银手铐了

    def get_one(self, sql):
        result = 0
        try:
            self.connect()
            self.cursor.execute(sql)
            result = self.cursor.fetchone()  # todo:利用游标只要一个结果
            self.close()
        except Exception as e:  # todo:获取异常信息别名e
            print('select error', e)
        return result  # todo:sql语句有返回值就出来 没有就返回异常信息

    def get_all(self, sql):
        result = 0
        try:
            self.connect()
            self.cursor.execute(sql)
            result = self.cursor.fetchall()
            self.close()
        except Exception as e:
            print('select error', e)
        return result  # todo:跟get_one()没有区别就是一个fechone,一个fechall

    def _edit(self, sql):
        result = 1
        try:
            self.connect()
            self.cursor.execute(sql)
            self.db.commit()
            self.close()
        except Exception as e:
            print('erroe', e)
            result = 0
            self.db.rollback()
        return result  # todo:如果sql语句正确那就提交返回result=0,语句执行存在异常就回滚一下保证库内数据正确返回result=1

    """_edit(self,sql) 其实就是做个判断,看看sql语句对不对,对就填进去,不对也不会影响库 下面的增删改都是基于这个方法"""

    def _insert(self, sql):
        return self._edit(sql)

    def _update(self, sql):
        return self._edit(sql)

    def _delect(self, sql):
        return self._edit(sql)


print(DB("biexianmafan").get_one("select * from biexianmafan;"))

  • 0
    点赞
  • 1
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值