编写base_dao.py 放在com.dao包里
import configparser
import pymysql
# 创建DAO基类
class BaseDao(object):
# 创建数据库配置文件构造方法
def __init__(self):
# 读取配置文件
self.config = configparser.ConfigParser()
self.config.read('config.ini', encoding='utf-8')
host = self.config['db']['host']
port = self.config.getint('db', 'port')
user = self.config['db']['user']
password = self.config['db']['password']
database = self.config['db']['database']
charset = self.config['db']['charset']
# 创建数据库链接对象conn
self.conn = pymysql.connect(host=host,
port=port,
user=user,
password=password,
database=database,
charset=charset)
# 创建数据库关闭实例方法
def close(self):
self.conn.close()