import pymysql class Con_db(object): db = [] # 设置连接池 __instance = None def __new__(cls, *args, **kwargs): if not cls.__instance:#判断单例是否存在,不存在就创建否则直接返回 cls.__instance = object.__new__(cls) return cls.__instance def __init__(self): self.db_host = '127.0.0.1' #主机地址 self.db_user = 'root' #用户名 self.db_password = '123456' #密码 self.db_database = 'test' #要操作的数据库 self.db = pymysql.connect(host=self.db_host, user=self.db_user, port=3306, database=self.db_database, password=self.db_password, charset='utf8') class AA(Con_db): def __init__(self): super().__init__() def get_data(self): sql = 'select * fro
python 单例模式连接数据库
最新推荐文章于 2020-12-15 23:35:08 发布
![](https://img-home.csdnimg.cn/images/20240711042549.png)