更新 connection.py
parent
b47b0cb8b9
commit
ba6aa5fbb5
|
|
@ -20,16 +20,3 @@ class MySQLPool:
|
||||||
def get_connection(self):
|
def get_connection(self):
|
||||||
# 从连接池中获取一个连接
|
# 从连接池中获取一个连接
|
||||||
return self.pool.connection()
|
return self.pool.connection()
|
||||||
|
|
||||||
# 使用上下文管理器自动处理连接的开启和关闭
|
|
||||||
def execute(self, sql, args=None):
|
|
||||||
with self.get_connection() as connection:
|
|
||||||
with connection.cursor() as cursor:
|
|
||||||
cursor.execute(sql, args)
|
|
||||||
if sql.strip().lower().startswith("select"):
|
|
||||||
# 如果是查询操作,返回所有结果
|
|
||||||
return cursor.fetchall()
|
|
||||||
else:
|
|
||||||
# 如果是增、删、改操作,提交事务并返回影响的行数
|
|
||||||
connection.commit()
|
|
||||||
return cursor.rowcount
|
|
||||||
Reference in New Issue