pymysql连接校验

 1 class DataSource(object):
 2 
 3     def __init__(self):
 4         self.conn = self.to_connect()
 5 
 6     def __del__(self):
 7         self.conn.close()
 8 
 9     # @staticmethod
10     def to_connect(self):
11         return pymysql.connect(host="localhost", user="root", passwd="123456", database="task_dialogue")
12 
13     def is_connected(self):
14         """Check if the server is alive"""
15         try:
16             self.conn.ping(reconnect=True)
17             # print"db is connecting"
18         except:
19             # traceback.print_exc()
20             self.conn = self.to_connect()
21             print("db reconnect")


# 使用方法
db = DataSourece()
db.is_connected()
connect = db.conn

 

上一篇:使用python操作mysql数据库


下一篇:django常见基础配置