Skip to content

Commit

Permalink
设置pymysql每次连接检查是否断开
Browse files Browse the repository at this point in the history
  • Loading branch information
wintests committed Apr 25, 2020
1 parent bf790c0 commit 11a4837
Showing 1 changed file with 4 additions and 0 deletions.
4 changes: 4 additions & 0 deletions common/mysql_operate.py
Original file line number Diff line number Diff line change
Expand Up @@ -23,6 +23,8 @@ def __del__(self): # 对象资源被释放时触发,在对象即将被删除

def select_db(self, sql):
"""查询"""
# 检查连接是否断开,如果断开就进行重连
self.conn.ping(reconnect=True)
# 使用 execute() 执行sql
self.cur.execute(sql)
# 使用 fetchall() 获取查询结果
Expand All @@ -32,6 +34,8 @@ def select_db(self, sql):
def execute_db(self, sql):
"""更新/新增/删除"""
try:
# 检查连接是否断开,如果断开就进行重连
self.conn.ping(reconnect=True)
# 使用 execute() 执行sql
self.cur.execute(sql)
# 提交事务
Expand Down

0 comments on commit 11a4837

Please sign in to comment.