DB_config.py 1.72 KB
Newer Older
aHuanYo committed
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73
# -*-coding:utf-8 -*- #
# ---------------------------------------------------------------------------
# ProjectName:   pythonProject2
# FileName:      DB_config.py
# Author:        YH
# Datetime:      2024/7/13 9:44
# Description:
# ---------------------------------------------------------------------------
import pymysql

from MyAPIAutoTest_v1.common.read_ini import ReadCommonIni


class DbConfig:
    def __init__(self):
        ini = ReadCommonIni()
        try:
            self.conn = pymysql.connect(
                host=ini.get_mysql_confdata("host"),
                port=int(ini.get_mysql_confdata("port")),
                user=ini.get_mysql_confdata("user"),
                password=ini.get_mysql_confdata("pwd"),
                database=ini.get_mysql_confdata("database"),
                charset="utf8"
            )
        except Exception as e:
            print("数据库连接失败!")
            raise e
        else:
            self.cur = self.conn.cursor()

    def sql_delete(self, sql_del):
        try:
            self.cur.execute(sql_del)
        except Exception as e:
            print("sql语句错误!")
            raise e
        else:
            self.conn.commit()

    def sql_select(self, sql_sel):
        try:
            self.cur.execute(sql_sel)
        except Exception as e:
            print("sql语句错误!")
            raise e
        else:
            data = self.cur.fetchall()
            if data:
                return data[0][0]

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


if __name__ == '__main__':
    db = DbConfig()
    sql = 'SELECT ID_ FROM uc_demension WHERE `CODE_`="test_dem_xyz_123";'
    print(db.sql_select(sql))
    db.db_close()