接口自动化----数据库处理

-*- coding: utf-8 -*-
 @Time : 2022-09-14 10:36
 @File : handle_db


import pymysql
from Common.handle_config import conf # 读取配置文件的数据


class HandleDB:
    def __init__(self):
        self.conn = pymysql.connect(
            host=conf.get("mysql", "host"),
            port=conf.getint("mysql", "port"),
            user=conf.get("mysql", "user"),
            passwd=conf.get("mysql", "password"),
            database=conf.get("mysql", "database"),
            charset="utf8",
            cursorclass=pymysql.cursors.DictCursor
        )
        self.cur = self.conn.cursor()  # 创建游标

    def select_one_data(self, sql):
        self.conn.commit()
        self.cur.execute(sql)
        return self.cur.fetchone()

    def select_all_data(self, sql):
        self.conn.commit()
        self.cur.execute(sql)
        return self.cur.fetchall()

    def get_count(self, sql):
        self.conn.commit()
        return self.cur.execute(sql)

    def updata(self, sql):
        """
        对数据库进行增、删、改的操作。
        :param sql:
        :return:
        """
        self.cur.execute(sql)
        self.conn.commit()

    def close(self):
        self.cur.close()
        self.conn.close()


if __name__ == '__main__':
    db = HandleDB()
    sql = "select * from member where mobile_phone = 13689681542"

    data = db.get_count(sql)
    print(data)

 

posted on 2022-09-26 15:53  秋秋秋~~  阅读(18)  评论(0)    收藏  举报

导航