pymysql装饰器封装
pymysql装饰器封装
def openClose(fun):
def run(sql=None):
coon =pymysql.connect(host='localhost' ,port=3306 ,user='root', password='1234qwer', db='test', charset='utf8')
cursor = coon.cursor()
try:
cursor.execute(fun( sql))
data = cursor.fetchall()
coon.commit()
print(data)
except Exception as e:
coon.rollback()
print('运行', str(fun), '方法时出现错误,错误代码:', e)
finally:
cursor.close()
coon.close()
return run @openClose
def runSql(sql=None):
if sql is None:
sql = 'select * from students1'
return sql runSql()
runSql(‘select * from students1‘ where name= ‘tom1’)

添加时间记录功能
添加时间记录功能
def openClose(fun):
def run(sql=None):
coon =pymysql.connect(host='localhost' ,port=3306 ,user='root', password='1234qwer', db='test', charset='utf8')
cursor = coon.cursor()
try:
start_time = time.time()
cursor.execute(fun( sql))
data = cursor.fetchall()
coon.commit()
end_time = time.time()
print('持续时间:'+str(end_time - start_time))
print(data)
except Exception as e:
coon.rollback()
print('运行', str(fun), '方法时出现错误,错误代码:', e)
finally:
cursor.close()
coon.close()
return run @openClose
def runSql(sql=None):
if sql is None:
sql = 'select * from students1'
return sql runSql()
输出:

open_and_close_db 重要!重要!重要!
open_and_close_db 重要!重要!重要! def open_and_close_db(do_sql):
def wrapper(sql='select * from students1'):
coon = pymysql.connect(host='localhost' ,port=3306 ,user='root', password='1234qwer', db='test', charset='utf8')
cursor = coon.cursor()
start_time = time.time()
do_sql(cursor, coon, sql)
cursor.close()
coon.close()
end_time = time.time()
print('持续时间:' + str(end_time - start_time))
return wrapper @open_and_close_db
def do_sql(cursor,coon,sql):
try:
cursor.execute(sql)
data = cursor.fetchall()
coon.commit()
print(data)
except Exception as e:
coon.rollback()
print('运行时出现错误,错误代码:', e) do_sql()
do_sql("update students1 set name = 'tom99999' where score = 44")
输出:


pymysql装饰器封装的更多相关文章
- python函数与方法装饰器
之前用python简单写了一下斐波那契数列的递归实现(如下),发现运行速度很慢. def fib_direct(n): assert n > 0, 'invalid n' if n < 3 ...
- Python之路(第十一篇)装饰器
一.什么是装饰器? 装饰器他人的器具,本身可以是任意可调用对象,被装饰者也可以是任意可调用对象. 强调装饰器的原则:1 不修改被装饰对象的源代码 2 不修改被装饰对象的调用方式 装饰器的目标:在遵循1 ...
- 面向对象之组合、封装、多态、property装饰器
概要: 组合 封装 property装饰器 多态 Python推崇鸭子类型:解耦合,统一标准(不用继承) 1. 组合 继承:会传递给子类强制属性 组合:解耦合,减少占用内存.如:正常继承,如果一个班级 ...
- 组合,多态与多态性,封装以及property装饰器介绍
一:组合: 什么是组合:组合指的是某一个对象拥有一个属性,该属性的值是另外一个类的对象. 为何要用组合:通过为某一个对象添加属性(属性的值是另外一个类的对象)的方式,可以间接地将两个类关联/整合/组合 ...
- Python10/24--组合/封装/property装饰器/多态
组合的应用: 1. 什么是组合 一个对象的属性是来自于另外一个类的对象,称之为组合 2. 为何用组合 组合也是用来解决类与类代码冗余的问题 3. 如何用组合 '''class Foo: aaa=111 ...
- 面向对象特征:封装、多态 以及 @propetry装饰器
(继承补充)组合 obj=fun()#对象 obj.attr=foo()#对象的属性等于另一个对象 什么是组合: A类的对象具备某一个属性,该属性的值是B类的对象 基于这种方式就把A类与B ...
- PYTHON-组合 封装 多态 property装饰器
# 组合'''软件重用的重要方式除了继承之外还有另外一种方式,即:组合组合指的是,在一个类中以另外一个类的对象作为数据属性,称为类的组合 1. 什么是组合 一个对象的属性是来自于另外一个类的对象,称之 ...
- 面向对象之 组合 封装 多态 property 装饰器
1.组合 什么是组合? 一个对象的属性是来自另一个类的对象,称之为组合 为什么要用组合 组合也是用来解决类与类代码冗余的问题 3.如何用组合 # obj1.xxx=obj2''''''# class ...
- 初识面向对象-封装、property装饰器、staticmathod(静态的方法)、classmethod(类方法) (五)
封装 # class Room:# def __init__(self,name,length,width):# self.__name = name# self.__length = length# ...
随机推荐
- mysql编译安装下载地址(官网)
https://dev.mysql.com/get/Downloads/MySQL-version number/mysql-version number.tar.gz 把这个地址上面的版本号改成自己 ...
- HttpURLConnection Get和Post发送数据
Get URL url = new URL(path); HttpURLConnection connection = (HttpURLConnection) url.openConnection() ...
- pyqt5--TableWidGet
使用pyqt5展示excel的数据到桌面,并获取选中的数据内容 from PyQt5 import QtCore, QtGui, QtWidgets from PyQt5.QtGui import Q ...
- go build -tags 的使用
go build 使用tag来实现编译不同的文件 go-tooling-workshop 中关于go build的讲解可以了解到go bulid的一些用法,这篇文章最后要求实现一个根据go bulid ...
- CentOS6.7搭建部署DNS服务 (详解主配置文件)
-DNS服务器的基本原理 域名简介:使用tcp/udp协议,默认使用53端口号 默认情况下,客户端发起的查询请求都是用UDP/53查询的. 默认情况下,从从服务器到主服务器传输数据用的是TCP/53. ...
- Python socket基本写法
TCP:长连接,安全可靠,不会丢包,但是会发生黏包,面向流的传输,超长的数据包会被拆包发送 多条send的小数据会连在一起发送,造成黏包现象,是tcp协议内部的优化算法造成的 如果要发送特别大的数据时 ...
- Beta冲刺阶段博客集合
Beta冲刺阶段博客集合 课程名称:软件工程1916|W(福州大学) 团队名称: 云打印 作业要求: 项目Beta冲刺(团队) 作业目标:作业集合 团队队员 队员学号 队员姓名 个人博客地址 备注 2 ...
- Ofbiz项目学习——阶段性小结——服务返回结果
一.返回成功 1.在.DispatcherReturnDemoService类中编写服务[returnSuccess],内容如下: /** * 返回成功结果 * @param dctx * @para ...
- react navite 学习资料
react 学习资料 https://github.com/crazycodeboy/GitHubPopular crazycodeboy/GitHubPopular https://github.c ...
- UI与数据的绑定
核心是数据变化跟踪与UI更新的问题 概念整理: 供业务使用的叫数据: 供UI使用的叫状态: UI的变化能被监听到: 数据的变化能实时反映到UI上: 数据变化—>拦截—〉UI状态重置—>UI ...