'''python 使用数据库-封装'''import pymysqlclass TestMysql(object): def __init__(self): self.a = None ##定义一个字典 dbConfig = { "host": "192.168.48.136", "port": 3306, "user": "chen", "passwd": "123456", "db": "test" } def get_connect(self): if self.a == None: # 函数传参 传入字典需要加** conn = pymysql.connect(**self.dbConfig) self.a = conn # 使用时不再建立新的链接,只需要使用已有的 def select(self): self.get_connect() print("select") def update(self): self.get_connect() print("update")def main(): testMysql = TestMysql() testMysql.select()if __name__ == '__main__': main()