1、mysql 的连接
import pymysql from pymysql.err import OperationalError import os import configparser base_dir = os.path.dirname(os.path.dirname(__file__)) file_path = os.path.join(base_dir,'db_config.ini') conf = configparser.ConfigParser() conf.read(file_path) host = conf.get('mysqlconf','host') port = conf.get('mysqlconf','port') db = conf.get('mysqlconf','db_name') user = conf.get('mysqlconf','user') password = conf.get('mysqlconf','password') class DB: def __init__(self): try: # 连接数据库 self.conn = pymysql.connect(host=host, user=user, password=password, db=db ) except OperationalError as e: print("Mysql Error %d:%s" % (e.args[0],e.args[1])) # 关闭数据库连接 def close(self): self.conn.close()