import pymysql class My_sql(): def __init__(self, host, user, passwd, db, port=3306, charset='utf8'): try: self.my_db = pymysql.connect(host=host, user=user, passwd=passwd, db=db, port=port, charset=charset, autocommit=True) except Exception as e: print('数据库连接失败,出错原因%s'% e) else: self.cur = self.my_db.cursor(cursor=pymysql.cursors.DictCursor)def __del__(self): self.cur.close() self.my_db.close()
__init__构造函数,常用于初始化
__del__析构函数,常用于程序执行结束时,系统自动调用执行一些关闭操作,此处请忽略,