# -*-coding:utf-8 -*- #
# ---------------------------------------------------------------------------
# ProjectName:   test61
# FileName:      db.py
# Author:       lao_zhao
# Datetime:     2024/7/12 14:10
# Description:
# 
# ---------------------------------------------------------------------------
import pymysql
from APIAutoTest_v3_1.common.read_basic_ini import ReadBasicIni


class DB:
    def __init__(self):
        """链接数据库，获取链接对象，再获取游标对象"""
        ini = ReadBasicIni()
        self.conn = pymysql.connect(
            host=ini.sql_connect_msg("host"),
            port=int(ini.sql_connect_msg("port")),
            user=ini.sql_connect_msg("user"),
            password=ini.sql_connect_msg("pwd"),
            database=ini.sql_connect_msg("database"),
            charset="utf8"
        )
        self.cursor = self.conn.cursor()

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

    def delete(self, sql):
        """执行删除的sql语句"""
        try:
            self.cursor.execute(sql)
        except Exception as e:
            raise e
        else:
            self.conn.commit()

    def select(self, sql):
        """执行查询的sql语句，并返回查询的结果"""
        try:
            self.cursor.execute(sql)
        except Exception as e:
            raise e
        else:
            select_result = self.cursor.fetchall()
            if select_result:
                return select_result[0][0]